用 NSURLSessionDownloadTask 異步下載資源成功后,completionHandler 一般是在子線程回調(diào)骗卜,如果我們轉(zhuǎn)到其它線程用 moveItemAtPath 方法移動(dòng)文件的時(shí)候骇陈,偶爾會(huì)遇到錯(cuò)誤:
Error Domain=NSCocoaErrorDomain Code=4 "“CFNetworkDownload_GAfbOO.tmp” couldn’t be moved to “某個(gè)文件夾名字” 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/某個(gè)文件夾名字/b60553f25575808c2d876c274de908a5, NSFilePath=/private/var/mobile/Containers/Data/Application/DA1DFC03-1FD9-448C-BCE7-4B345FC2057D/tmp/CFNetworkDownload_GAfbOO.tmp, NSUnderlyingError=0x17405
解決方法震庭,不要轉(zhuǎn)移到其它線程,直接在 completionHandler 線程 moveItemAtPath 轉(zhuǎn)移文件你雌。