8

I am implementing background refresh in my app. To test the app I have this in my AppDelegate.m:

-(void)application:(UIApplication *)application
performFetchWithCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {



    NSLog(@"did a background refresh");
    completionHandler(UIBackgroundFetchResultNewData);



}

I tested it on the simulator and it works fine. I am unsure of what the use of completionHandler(UIBackgroundFetchResultNewData); Why do I need to call this and what can I use it for? I understand that it can be:

UIBackgroundFetchResult.NewData - Called when new content has been fetched, and the application has been updated.
UIBackgroundFetchResult.NoData - Called when the fetch for new content went through, but no content is available.
UIBackgroundFetchResult.Failed - Useful for error handling, this is called when the fetch was unable to go through.

However I don't understand why I have to call it and how it's useful. Any pointers on this would be really appreciated. Thanks!

1 Answer 1

10

You have to call this to let iOS know what the result of your background fetch was. It uses this information to schedule future background fetches.

If you neglect to do this, future background fetches may be delayed by the OS.

Update: The consequences of not calling this handler might include terminating your app altogether.

7
  • Does it not also force the UI to update before the push is opened too?
    – Kex
    Jun 10, 2015 at 10:39
  • AFAIK it will not update your UI per se; but since you typically changed your UI yourself as a response to the new data, it also notifies the system that this has happened. This will, for example, affect the scaled-down image of your app in the task switcher. Jun 10, 2015 at 10:46
  • I noticed that when I receive a notification no matter what the status of the completion handler is it is executing code from the current view controller (even though the app is in the background). Not totally sure what code it's executing though.
    – Kex
    Jun 10, 2015 at 10:56
  • seems like it is executing viewWillAppear but the app is not even showing
    – Kex
    Jun 10, 2015 at 10:57
  • Yes, it does that to create that little snapshot (drawing your UI into a separate graphics context.) Jun 10, 2015 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.