NSURLSession didFinishDownloadingToURL temporary downloaded file not found

拟墨画扇 提交于 2021-02-07 05:58:19

问题


I'm having a strange issue with NSURLSession on the delegate method didFinishDownloadingToURL.

First thing I'm doing is check if the temporary downloaded file exist:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                                            didFinishDownloadingToURL:(NSURL *)location
{
    if (![[NSFileManager defaultManager] fileExistsAtPath: [location path]])
    {
        NSLog(@"Error. File not found");
        return; // is giving error when the app is wake up by the system
    }
    ...
}

It works normally when the app is in foreground and download finishes. But when the app is in background and is killed forcedly by the operating system, it returns false.

Does anyone have any idea about what might be happening? I know that there is a time limit for the execution of this delegate method when the app is wake up by the operating system, by it makes no sense for the temporary file to be not there. I can't even copy it to another location... Does it make sense to be because of the size of the file? I'm downloading a file of +-130MB.

Thanks.


回答1:


I solved the same problem by installing app after uninstall app. It seems that the NSURLSession leaves debris on system when forced shutdown happens while a network session working.




回答2:


From the Apple Docs:

(location is..) "A file URL for the temporary file. Because the file is temporary, you must either open the file for reading or move it to a permanent location in your app’s sandbox container directory before returning from this delegate method.

If you choose to open the file for reading, you should do the actual reading in another thread to avoid blocking the delegate queue."



来源:https://stackoverflow.com/questions/28860112/nsurlsession-didfinishdownloadingtourl-temporary-downloaded-file-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!