RE: NSRunLoop question
RE: NSRunLoop question
- Subject: RE: NSRunLoop question
- From: Mike Laster <email@hidden>
- Date: Fri, 09 Aug 2002 20:11:36 -0400
I solved my original problem. The solution is to collect all of my received
messages into an NSArray, and then process them in a DIFFERENT call out
triggered by performSelector:withObject:afterDelay:. This makes the message
handling code run in the context of a NSTimer callout instead of a CFSocket
callout, and NSTimer callouts seem to be able to safely run a nested run
loop.
I have ANOTHER run loop problem though. How do I force a run loop to
iterate?
I have the following code for my sub-runloop:
- (void) runWithTimeout:(NSTimeInterval)inTimeout terminationFlag:(BOOL
*)inFlag
{
NSRunLoop *rl = [NSRunLoop currentRunLoop];
NSDate *limitDate = [NSDate dateWithTimeIntervalSinceNow:inTimeout];
NSTimeInterval limitTimeInterval = [limitDate
timeIntervalSinceReferenceDate];
while ( *inFlag == NO && [NSDate timeIntervalSinceReferenceDate] <
limitTimeInterval)
{
NSLog(>>> Run subloop until condition or timeout");
[rl runMode:NSDefaultRunLoopMode beforeDate:limitDate];
NSLog(@"<<< Run subloop until condition or timeout");
}
}
The intention is that the subloop should be run until the timeout is
reached, or *inFlag is true. I set my timeout to 30.0 seconds, and can tell
from logging that *inFlag is getting set to TRUE just a fraction of a second
after I start the loop. The problem is that this while loop takes a full
30 seconds to terminate because runMode:beforeDate: never returns before
then. This seems to be happening because there are no more events to
process. If I force a network event like connecting another client, the
Loop terminates immediately like it should.
I tried the "obvious" choice and generated a fake event to force the loop
To cycle:
[self performSelector:@selector(self) withObject:nil afterDelay:0.0];
But that doesn't work. It seems that NSTimer events are internal and don't
Cycle runMode:beforeDate:...it has to be a port event of some sort.
I also tried:
CFRunLoopWakeUp([[NSRunLoop currentRunLoop] getCFRunLoop]);
It's not documented, but I figured that maybe it would do the obvious
thing...no luck.
It seems rather inefficient for my program to send a network packet to
itself just to unblock the run loop. There HAS to be a better way....
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.