【发布时间】:2014-10-17 18:19:24
【问题描述】:
我在IOS项目中使用后台文件下载,当文件下载开始时,进度视图的更新是通过block NSOperationQueue.mainQueue().addOperationWithBlock()在方法中开始的:
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown {
println("Unknown transfer size");
}
else{
let data = getDBInfoWithTaskIdentifier(downloadTask.taskIdentifier)
NSOperationQueue.mainQueue().addOperationWithBlock(){
data.db_info.downloadProgress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
let inf = data.db_info.indexPath
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: data.index, inSection: 0)) as SettingsTableViewCell
cell.downloadProgress.hidden = false
cell.downloadProgress.setProgress(data.db_info.downloadProgress, animated: true)
}
}
}
当带有下载 UI 的视图被关闭并再次出现时,self.tableView 是新对象,但在 NSOperationQueue.mainQueue() 操作中更新进度视图的 self.tableView 是旧对象,即关闭之前的内容。 Println() 返回两个不同的对象。 NSOperationQueue.mainQueue() 块中是否有可能更新有关 self.tableView 的数据?
【问题讨论】:
标签: ios xcode swift nsoperationqueue