In my iOS application I have registered the AppDelegate as notification listener for CoreData changes. With this piece of code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesFromContextDidSaveNotification:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:[self persistentStoreCoordinator]];
And the mergeChangesFromContextDidSaveNotification method get correctly called every time there's an update.
However inside this method I am trying to call an NSTimer for doing another operation:
- (void)mergeChangesFromContextDidSaveNotification:(NSNotification *)notification {
NSTimer *t =[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(mergeCoreDataFromCloud:)
userInfo:nil
repeats:NO];
}
}
and the point is that mergeCoreDataFromCloud: which should be fired by the timer is never called. This is the signature:
-(void)mergeCoreDataFromCloud:(NSTimer*)timer {
// never called...
}
please note that I am at early stage of development, the code is not perfect, and I am only interested in knowing why the timer is not started. I suppose it has something to do with threads, but I have no guess...
thanks
A timer relies on the run loop being run in the mode(s) in which it has been scheduled.
In a Cocoa app, the main thread runs its run loop automatically. However, no other thread can be relied on to run its run loop automatically.
So, usually you want to schedule timers on the main thread. You can also schedule them on a thread you create and control, which you cause to run its run loop in an appropriate mode.
There's no enough information in your question to know what thread this code is being called on.