For a game I'm developing, I call an expensive method from one of the touch processing routines. In order to make it faster, I decided to use performSelectorInBackgroundThread
, so instead of:
[gameModel processPendingNotifications];
I switched to:
[gameModel performSelectorInBackground:@selector(processPendingNotifications) withObject:nil];
The first problem I had, is that processPendingNotifications
did not have a NSRunLoop
, so I added it, like this:
- (void)processPendingNotifications {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pendingNotificationsQueue makeObjectsPerformSelector:@selector(main)];
[pendingNotificationsQueue removeAllObjects];
[pool drain];
}
So far so good. My problem is that some of the methods that are called from this background thread create new NSTimer
instances. These instances, end up not firing. I think this is because the secondary thread I have doesn't have a (or is ending its) NSRunLoop
. I'm starting the new timers by using:
[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
My questions are:
- Am I on the right path of suspecting the problem has to do with the
NSRunLoop
? - Is there a way I can start a
NSTimer
from a background thread and attach it to the main thread'sNSRunLoop
?