[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: NSNotification and NSRunLoop
From: |
Richard Frith-Macdonald |
Subject: |
Re: NSNotification and NSRunLoop |
Date: |
Wed, 29 Oct 2003 06:18:51 +0000 |
On Wednesday, October 29, 2003, at 01:29 AM, Yen-Ju Chen wrote:
Hi,
I try to send NSNotification via NSDistributedNotificationCenter to
a GNUstep tool which use while(...){} to keep alive.
I notice that all the NSNotification reach the tool after the while
loop stop.
Why does it happen ?
Do I have to use NSRunLoop in order to receive NSNotification in the
middle of while loop ?
Yes ... events only arrive while a run loop is running ... so you need
to run the runloop
within your while loop.
How to do that ?
Something like this ?
while (...) {
[[NSRunLoop currentLoop] runUntilDate: [NSData distantFuture]];
}
Then how far the [NSData distantFuture] would be ? More than one
second ?
I would recommend using -runMode:beforeDate: ne each time round the
loop,
unlike -runUntilDate:, this method processes the runloop only once
before returning
cntrol to you while loop.
As to the date ... you probably don't want -distantFuture as thats a
long way ahead
(not in our lifetime). I'd guess that you want a date a fraction of a
second in the
future, so you can execute the other code in your while loop
frequently. You should
create and destroy an autorelease pool each time round the loop, to
avoid
running out of memory because you create a date object each time round
the loop.
eg.
while (1)
{
CREATE_AUTORELEASE_POOL(pool);
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
beforeDate:
[NSDate dateWithTimeIntervalSinceNow: 0.1]];
// Your other code here
RELEASE(pool);
}
If, on the other hand, your while loop contains no other code, and is
intended
to run forever, you could just do [[NSRunLoop currentRunLoop] run] in
place
of the entire while loop.