gpt4 book ai didi

ios - didReceiveRemoteNotification : fetchCompletionHandler: download freezes

转载 作者:行者123 更新时间:2023-11-29 12:13:16 26 4
gpt4 key购买 nike

我们的应用程序利用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com