当第一次使用或应用程序进入后台时,下载失败



Alamofire 3.5.0, Swift2.2

我下载ZIP文件与Alamofire下载方法,我注意到当我开始下载进程和应用程序到后台比下载失败与以下错误:

----------------------
error Optional(Error Domain=NSCocoaErrorDomain Code=4
    "“CFNetworkDownload_pZ56nc.tmp” couldn’t be moved to “courses”
    because either the former doesn't exist, or the folder containing
    the latter doesn't exist." UserInfo=
    {NSSourceFilePathErrorKey=
/private/var/mobile/Containers/Data/Application/[UUID]/tmp/CFNetworkDownload_pZ56nc.tmp, 
    NSUserStringVariant=(
        Move
    ),     
    NSDestinationFilePath=
/var/mobile/Containers/Data/Application/[UUID]/Library/courses/course_302.zip, 
    NSFilePath=
/private/var/mobile/Containers/Data/Application/[UUID]/tmp/CFNetworkDownload_pZ56nc.tmp, 
    NSUnderlyingError=0x13f52f990 {Error Domain=NSPOSIXErrorDomain
    Code=2 "No such file or directory"}})
----------------------

这是下载文件的代码:

//...
var userLibraryPath:String = {
    return NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
}()
//...
let _coursePath:NSURL = NSURL(string: "file://(userLibraryPath)/)")!
//...
let zipURL = _coursePath.URLByAppendingPathComponent("course_(courseId).zip")
        //if file exists destroy it
        if let zipPath = zipURL?.path where NSFileManager.defaultManager().fileExistsAtPath(zipPath) {
            do {
                try NSFileManager.defaultManager().removeItemAtPath(zipPath)
            } catch let error as NSError {
                print(error)
            }
        }
        //
        let downloadRequest = Alamofire.download(Router.download(courseId), destination: { (url:NSURL, urlResponse:NSHTTPURLResponse) -> NSURL in
            //
            return zipURL!
            //
        }).progress({ (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
            //
            let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
            dispatch_async(GlobalMainQueue, {
                self.notifyDownloadProgress(courseId, progress: progress)
            })
        }).response(completionHandler: { (request:NSURLRequest?, response:NSHTTPURLResponse?, data:NSData?, error:NSError?) in
            self.removeFromQueue(courseId)
            print("response")
            print("----------------------")
            print("error (error)")
            print("----------------------")
            //here I would try to extract it
})

UPDATE我刚刚在iPhone 5上测试了新安装的应用程序,它不需要进入后台(例如通过home键)就会失败,它在第一次加载(以及任何后续)时失败,直到应用程序被杀死并重新打开。

为什么"/private"位被添加到路径中?我哪里做错了?

确实是"No such file or directory"错误。

当我添加:

//
let downloadRequest = Alamofire.download(Router.download(courseId), destination: { (url:NSURL, urlResponse:NSHTTPURLResponse) -> NSURL in
    let course = zipURL!.URLByDeletingLastPathComponent!.path!
    let fm = NSFileManager.defaultManager()
    var isDir:ObjCBool = false
    if(fm.fileExistsAtPath(path, isDirectory: &isDir) == false){
       //doesnt exist
       do {
           try fm.createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
       } catch let error as NSError {
           //
           print(error)
       }
    }
    return zipURL!
    //
 })

最新更新