3

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?

SchroedingersCat
  • 487
  • 8
  • 21

3 Answers3

4

1) In order for the app to run in the background you should be using one of the modes mentioned in the "Background Execution and Multitasking section here:

  • uses location services
  • records or plays audio
  • provides VOIP services
  • background refresh
  • connection to external devices like through BLE

If you are not using any of the above, it is not possible to get asynchronous events in the background.

2) In order to see the crash logs/call stack place an exception breakpoint or look into the "Device Logs" section here: Window->Organizer->Devices->"Device Name" on left->Device Logs on Xcode.

dagnytaggart
  • 511
  • 7
  • 17
1

To answer your first question, take a look at https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

What I did to get code running in the background is to do something like

In the .h file

UIBackgroundTaskIdentifier backgroundUploadTask;

In the .m file

-(void) functionYouWantToRunInTheBackground
{    
    self.backgroundUploadTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
}];
//code to do something
}

-(void) endBackgroundUpdateTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUploadTask];
    self.backgroundUploadTask = UIBackgroundTaskInvalid;
}

The code above I pretty much learned from objective c - Proper use of beginBackgroundTaskWithExpirationHandler

As for your second question, you should set a breakpoint where code is supposed to run when you bring the app back to the foreground. No one can figure out why an app crashes if not given enough code or information.

Community
  • 1
  • 1
Rick
  • 389
  • 3
  • 8
  • The error I am getting when the App comes back is EXC_BAD_ACCESS Code=1. That is telling me (I think) that when the app is going to background (or suspend) it is cleaning up the object containing the code in the call back. How does one declare an object so that it does not get cleaned up? – SchroedingersCat Jun 24 '14 at 11:02
  • Is NSNotificationCenter and Local Notifications part of the same thing? According to the docs it IS possible for a local notification to bring an app from Suspend to Background to execute the callback, but is not clear how to make that happen. The docs also say that all Notifications (meaning those through NotificationCenter) are queued while the app is suspended. So the question is how to bypass the queueing of those notifications? – SchroedingersCat Jun 24 '14 at 11:06
  • By local notifications do you mean `UILocalNotification`? Those are scheduled push notification that the app on your phone will send. So what you are trying to do is get the app to listen for localcalendarchanges in the background? Do you want to send a push notification when it happens? – Rick Jun 24 '14 at 17:26
  • Yes, I meant UILocalNotification but I wanted to know if that is what the Calendar App is sending to my app and whether UILocalNotification and NSNotificationCenter are the related -- it sounds like they aren't. – SchroedingersCat Jun 25 '14 at 10:55
  • I'm not too sure what you mean about related but those are two different objects, used for different things. – Rick Jun 25 '14 at 14:23
1

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.

SchroedingersCat
  • 487
  • 8
  • 21