ios - didReceiveRemoteNotification : fetchCompletionHandler: download freezes

标签 ios objective-c push-notification afnetworking

我们的应用程序利用 didReceiveRemoteNotification: fetchCompletionHandler: 方法 UIApplicationDelegate 在收到 content-available 推送通知时开始下载。

应用程序执行的各个步骤:

[1]接收通知

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

[2]开始新的下载任务

- (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] 处理下载

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

这会产生如下日志:

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!

但有时(完全随机)我会看到这样的日志:

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!

换句话说,下载永远不会完成,看起来应用程序卡住了它的后台 session 。因为当我将应用程序放回前台时,下载会在某个时候完成。

有没有人见过这种行为?
仅供引用:我启用了正确的功能,使用了正确的配置文件,并且该应用程序有权在后台运行。

更新: 我们在 AFNetworking 2.0 and background transfers 中使用了答案/评论开发后台管理器时

最佳答案

您正在开始下载时调用完成处理程序!我很确定你应该在下载完成时调用完成处理程序,而不是在下载开始后 2 秒!换句话说,您应该在 self setDownloadTaskDidFinishDownloadingBlock... 中调用 completionHandler(UIBackgroundFetchResultNewData); 并以某种方式传递 CompletionBlock。

[self setDownloadTaskDidFinishDownloadingBlock:^NSURL *(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location) {

  NSLog(@"Download complete!");
  // ... Create save path
 NSLog(@"Completion handler!");
 completionHandler(UIBackgroundFetchResultNewData);

  return path;
}];

正确的日志应该是这样的:

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] Download complete!

2015-09-21 15:39:06.315 App[315:20933] Completion handler!

关于ios - didReceiveRemoteNotification : fetchCompletionHandler: download freezes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697291/

相关文章:

ios - 苹果推送通知 registerForRemoteNotificationTypes : method

ios - 在设置应用程序中看不到MyAppNotificationSettings选项

objective-c - Apple iOS ASlog,轮询消息.. [代码]

ios - UITableView 部分索引针对 iOS 7 中的错误单元格,顶行隐藏在导航栏下方

ios - 无法从 SQL 数据库检索查询

iOS:仅当 APP 处于事件状态时重复任务(无后台)

ios - 应用程序是从图标或通知打开的

iphone - 当我们滚动自定义表格时,iPhone 中的单元格内容会发生变化

ios - 使用 sinch 进行语音通话

objective-c - 为什么 m_array.count 在用作 for 循环中的条件时会失败?