didReceiveRemoteNotification:fetchCompletionHandler:下载冻结 [英] didReceiveRemoteNotification: fetchCompletionHandler: download freezes
问题描述
我们的应用程序利用 didReceiveRemoteNotification:fetchCompletionHandler:
方法 UIApplicationDelegate
在时开始下载收到内容可用
推送通知。
Our app utilizes didReceiveRemoteNotification: fetchCompletionHandler:
methods UIApplicationDelegate
to start a download when a content-available
push notification is received.
应用执行的各个步骤:
[1]接收通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
NSLog(@"Remote download push notification received");
[AFHTTPSessionManager_Instance downloadFile:completionHandler];
}
[2]开始新的下载任务
[2] Start 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]处理下载
[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!
但有时(完全随机),我看到这样的日志:
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 looks like the app freezes its background session. Because when I put the app back into the foreground the download will finish at some point.
有人以前见过这种行为吗?
仅供参考:启用了正确的功能,使用了正确的配置文件,并且该应用有权在后台运行。
Has anyone seen this behavior before?
FYI: I enabled the right capabilities, used a correct provision profile and the app has permissions to run in the background.
更新:
我们开发背景时,在 AFNetworking 2.0和背景传输中使用了答案/评论经理
Update: We've used answers/comments in AFNetworking 2.0 and background transfers when developing the background manager
推荐答案
您要在开始下载时调用完成处理程序!我很确定您应该在下载完成后(而不是下载开始后的2秒内)致电完成处理程序!换句话说,您应该在 self setDownloadTaskDidFinishDownloadingBlock ...
内部调用 completionHandler(UIBackgroundFetchResultNewData);
,然后通过CompletionBlock
You are calling the completion handler upon starting download! I'm pretty sure you should call the completion handler when the download is finished not 2 seconds after download started! in other words, you should call completionHandler(UIBackgroundFetchResultNewData);
inside self setDownloadTaskDidFinishDownloadingBlock...
and of corse pass the 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 be something like this:
2015-09-21 15:38:57.145 App [315:20933]远程下载推送
收到通知
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]开始下载
http:// domain.com/file.mov
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]下载完成!
2015-09-21 15:38:59.654 App[315:20933] Download complete!
2015-09-21 15:39:06.315 App [315:20933]完成处理程序!
2015-09-21 15:39:06.315 App[315:20933] Completion handler!
这篇关于didReceiveRemoteNotification:fetchCompletionHandler:下载冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!