views:

88

answers:

1

I'm writing a Cocoa application, with a GUI designed in Interface Builder. I need to schedule background activity (at regular intervals) without blocking the UI, so I run it in a separate thread, like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [self performSelectorInBackground:@selector(schedule) withObject:nil];
}

- (void) schedule {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];

    timer = [[NSTimer scheduledTimerWithTimeInterval:FEED_UPDATE_INTERVAL
                                                  target:activityObj
                                        selector:@selector(run:)
                                        userInfo:nil
                                         repeats:YES]
         retain];

    [runLoop run];
    [pool release];
}

I retain the timer, so I can easily invalidate and reschedule.

Problem: I must also fire the run: method in response to GUI events, so it is synchronous (i.e. a "perform activity" button). Like this:

[timer fire];

I could do this with performSelectorInBackground too, and of course it doesn't block the UI. But this synchronous firings run in another runloop! So I have no guarantee that they won't overlap. How can I queue all of my firings on the same runloop?

A: 
[timer setFireDate:[NSDate distantPast]];

I obtained the desired effect by adjusting the next fire date to be ASAP, by passing a past date to setFireDate.

mips