我正在使用以下代码下载约 200mb 的播客并将其写入 Documents 目录:
var podcastRequest = NSURLRequest(URL: audioUrl)
NSURLConnection.sendAsynchronousRequest(podcastRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if let myPodcastDataFromUrl = NSData(contentsOfURL: audioUrl) {
if myPodcastDataFromUrl.writeToURL(destinationUrl, atomically: true) {
// add to the array to track the download
var tempDic = self.posts[theRow] as! NSMutableDictionary as NSMutableDictionary
tempDic["downloaded"] = "true"
self.posts[theRow] = tempDic
} else {
println("Error saving file")
}
}
})
sendAsynchronousRequest 调用可防止在下载期间发生锁定,但应用程序在开始实际将其写入目录时仍会冻结。
有没有办法完全防止锁定发生,或者我将不得不一次写入更小的块?