iOS NSURLSessionDownloadTask 资源下载成功,moveItemAtPath 失败的问题

用 NSURLSessionDownloadTask 异步下载资源成功后,completionHandler 一般是在子线程回调,如果我们转到其它线程用 moveItemAtPath 方法移动文件的时候,偶尔会遇到错误:

Error Domain=NSCocoaErrorDomain Code=4 "“CFNetworkDownload_GAfbOO.tmp” couldn’t be moved to “某个文件夹名字” because either the former doesn't exist, or the folder containing the latter doesn't exist." UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/DA1DFC03-1FD9-448C-BCE7-4B345FC2057D/tmp/CFNetworkDownload_GAfbOO.tmp, NSUserStringVariant=(
Move
), NSDestinationFilePath=/var/mobile/Containers/Data/Application/DA1DFC03-1FD9-448C-BCE7-4B345FC2057D/Library/Caches/某个文件夹名字/b60553f25575808c2d876c274de908a5, NSFilePath=/private/var/mobile/Containers/Data/Application/DA1DFC03-1FD9-448C-BCE7-4B345FC2057D/tmp/CFNetworkDownload_GAfbOO.tmp, NSUnderlyingError=0x17405

解决方法,不要转移到其它线程,直接在 completionHandler 线程 moveItemAtPath 转移文件。

你可能感兴趣的:(iOS NSURLSessionDownloadTask 资源下载成功,moveItemAtPath 失败的问题)