Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of completionHandler(UIBackgroundFetchResultNewData) when doing a background refresh in iOS

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!

like image 324
Kex Avatar asked Jun 10 '15 10:06

Kex


1 Answers

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.

like image 129
fbitterlich Avatar answered Nov 20 '22 15:11

fbitterlich