DidReceiveRemoteNotification: fetchCompletionHandler: download freezes

Our application uses didReceiveRemoteNotification: fetchCompletionHandler: UIApplicationDelegate to start loading when a push content-available notification is received.

Individual steps that the application takes:

[1] Get notified

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { NSLog(@"Remote download push notification received"); [AFHTTPSessionManager_Instance downloadFile:completionHandler]; } 

[2] Launch a new download task

 - (void)downloadFile:(void (^)(UIBackgroundFetchResult))completionHandler { NSURLSessionDownloadTask *task = [AFHTTPSessionManager_Instance downloadTaskWithRequest:request progress:nil destination:nil completionHandler:nil]; [task resume]; NSLog(@"Starting download of %@", request.URL.absoluteString); // Some additional time to start the download dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ NSLog(@"Completion handler!"); completionHandler(UIBackgroundFetchResultNewData); }); 

[3] Refer to the download

 [self setDownloadTaskDidFinishDownloadingBlock:^NSURL *(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location) { NSLog(@"Download complete!"); // ... Create save path return path; }]; 

The result is a log like this:

 2015-09-21 15:38:57.145 App[315:20933] Remote download push notification received 2015-09-21 15:38:57.485 App[315:20933] Starting download of http://domain.com/file.mov 2015-09-21 15:38:59.654 App[315:20933] Completion handler! 2015-09-21 15:39:06.315 App[315:20933] Download complete! 

But sometimes (completely random) I see logs like this:

 2015-09-21 15:38:57.145 App[315:20933] Remote download push notification received 2015-09-21 15:38:57.485 App[315:20933] Starting download of http://domain.com/file.mov 2015-09-21 15:38:59.654 App[315:20933] Completion handler! 

In other words, the download never completes, it seems the application is freezing its background session. Because when I bring the application back to the forefront, the download will be completed at some point.

Has anyone seen this behavior before?
FYI: I enabled the correct features, used the correct profile profile, and the application has permissions to run in the background.

Update: We used answers / comments in AFNetworking 2.0 and background transfers when developing the background manager

+1
ios objective-c push-notification afnetworking
Sep 21 '15 at 2:04
source share
1 answer

You call the completion handler at boot! I am sure that you should call the completion handler when the download is completed not 2 seconds after the start of the download! in other words, you should call completionHandler(UIBackgroundFetchResultNewData); inside self setDownloadTaskDidFinishDownloadingBlock... and corse skip CompletionBlock somehow.

 [self setDownloadTaskDidFinishDownloadingBlock:^NSURL *(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location) { NSLog(@"Download complete!"); // ... Create save path NSLog(@"Completion handler!"); completionHandler(UIBackgroundFetchResultNewData); return path; }]; 

The correct log should look something like this:

2015-09-21 15: 38: 57.145 Application [315: 20933] Remote download received notification

2015-09-21 15: 38: 57.485 App [315: 20933] Start of downloading http://domain.com/file.mov

2015-09-21 15: 38: 59.654 App [315: 20933] Download the full version!

2015-09-21 15: 39: 06.315 Application [315: 20933] The completion handler!

+1
Sep 21 '15 at 14:11
source share



All Articles