没有从URLSession返回的位置数据(会话:downloadTask:didFinishDownloadingToURL位置)

时间:2015-10-22 18:00:54

标签: ios swift nsurlsessiondownloadtask

我正在使用标准后台会话来处理后台提取。操作开始很好并回调:

URLSession(session:downloadTask:didFinishDownloadingToURL location)

经常被调用,但是当我检查位置url的内容时,我发现没有。我检查了正在处理的网址,这是一个很好的网址,并且在浏览器上提交时正确返回数据。可能是什么?我知道我需要将数据复制到临时缓存中,但是当它仍然存在时我立即检查了它的内容。这是我的代码:

let span:Float=0.005
func handleRemoteFetch(completionHandler: ((UIBackgroundFetchResult) -> Void)?){
    fetchCompletion=completionHandler;
    fetchInitiation={[unowned self] in
        let theRequest=mapDelegate.sharedMapDelegate().requestForRefreshAndLocation(self.presentLocation!, span:self.span)
        let task = self.backgroundSession.downloadTaskWithRequest(theRequest)
        task.resume()
        print(theRequest.description)
    }
    if (presentLocation != nil){
        fetchInitiation!()
        fetchInitiation=nil
    }
}

func URLSession(session: NSURLSession,
    downloadTask: NSURLSessionDownloadTask,
    didFinishDownloadingToURL location: NSURL){
        print(downloadTask.originalRequest!.description)
        let data = NSData(contentsOfURL: location)
        if data != nil {
            let str = String(data:data!, encoding:NSUTF8StringEncoding)
            print(str);
        } else {
            print("empty output")
        }
}

1 个答案:

答案 0 :(得分:2)

当应用程序在物理设备上执行时,问题并未出现。一段时间后,它也开始在模拟器上工作,没有令人信服的理由。