Search code examples
iosobjective-cnsnotificationcenterekeventkit

NSNotificationCenter callback while app in background


One question and one issue: I have the following code:

- (void) registerForLocalCalendarChanges
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localCalendarStoreChanged) name:EKEventStoreChangedNotification object:store ];

}

- (void) localCalendarStoreChanged
{
    // This gets call when an event in store changes
    // you have to go through the calendar to look for changes
    [self getCalendarEvents];
}

These methods are in a class/object called CalendarEventReporter which contains the method getCalendarEvents (in the callback).

Two things: 1) If the app is in the background the callback does not run. Is there a way to make it do that? 2) When I bring the app back into the foreground (after having changed the calendar on the device) the app crashes without any error message in the debug window or on the device. My guess is that the CalendarEventReporter object that contains the callback is being garbage-collected. Is that possible? Any other thoughts on what might be causing the crash? Or how to see any error messages?


Solution

  • The solution to the second part of the question was to raise the scope of the object containing the callback code. I raised it to the level of the containing ViewController. This seems to work. I still can't figure out how to raise the Notification (i.e. execute the call back) if the notification comes while the app is in the background/suspended. This prevented the object containing the callback from being cleaned up.