下载停止时关闭ViewController
我正在使用Cheapjack Library (iOS)下载我的应用程序在Swift中的文件。我从另一个班级排队下载。这似乎工作正常,下载排队并开始下载。但是,当下载发生的“DownloadsViewController”未显示时,文件完成下载时不会执行didiFinishDownload块。关于如何处理我的错误的任何想法。 另外,当我打开下载视图控制器并解雇它时,下载会挂起它的位置,当我解除ViewController 任何想法?下载停止时关闭ViewController
这里是我的代码:如果您关闭它
//Adding download from another class
let downloadItem = DownloadsTableViewCellItem(identifier: identifier, urlString: url3, infoLabelTitle: info, stateLabelTitle: "Downloading...", progressLabelTitle: "", action: DownloadsTableViewCellAction.Pause)
DownloadsViewController().addDownloadItem(downloadItem, withIdentifier: identifier)
//Receiving download in DownloadsViewController
func addDownloadItem(downloadItem: DownloadsTableViewCellItem, withIdentifier identifier: CheapjackFile.Identifier) {
downloadItems[identifier] = downloadItem
identifiers.append(identifier)
CheapjackManager.sharedManager.download(downloadItem.url(), identifier: identifier)
self.tableView.reloadData()
}
//DidFinishBlock
func cheapjackManager(manager: CheapjackManager, didFinishDownloading withSession: NSURLSession, downloadTask: NSURLSessionDownloadTask, url: NSURL, forFile file: CheapjackFile) {
dispatch_async(dispatch_get_main_queue()) {
print("Did finish downloading file")
SongsTableViewController().tableView.reloadData()
let filemanager = NSFileManager.defaultManager()
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
print("DocumentsPath: \(documentsPath)")
let randomUUID: String = NSUUID().UUIDString + ".mp3"
let destinationPath = documentsPath.stringByAppendingString("/" + randomUUID)
print("DestinationPath: \(destinationPath)")
if (!filemanager.fileExistsAtPath(destinationPath as String)) {
let url1 = file.request.URL
print("URL1: \(url1)")
print("Temp Loc: \(String(url))")
let data = NSData(contentsOfURL: url)
if (data != nil) {
data!.writeToFile(destinationPath, atomically: false)
//data!.writeToURL(NSURL(string:destinationPath)!, atomically: false)
print("The music files has been saved.")
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let fetchRequest = NSFetchRequest(entityName: "Track")
fetchRequest.predicate = NSPredicate(format: "url = %@", String(url1!))
let context = appDel.managedObjectContext
do {
let results = try context.executeFetchRequest(fetchRequest)
fetchResults = results as! [NSManagedObject]
if fetchResults.count != 0 {
let managedObject = fetchResults[0]
managedObject.setValue(destinationPath, forKey: "fileURL")
appDel.saveContext()
SongsTableViewController().fetchData()
} else {
print("No track found")
}
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
} else {
print("The files already exist")
}
}
}
DownloadsViewController
将DEINIT。
请注意,delegate
的CheapjackManager
是weak
。在您解雇DownloadsViewController
后,delegate
将被设置为零,任何呼叫delegate
都将被忽略,包括cheapjackManager:didiFinishDownload:
。
与此同时,CheapjackManager
仍在为您下载资源。只是不能在DownloadsViewController
中观察它。
我可以避免deinit调用吗? – He1nr1ch
尝试将委托设置为某个其他实例,例如'AppDelegate'。 – user3480295
非常感谢你,似乎在伎俩。 – He1nr1ch
您是否将CheapjackManager.sharedManager的代理设置为'DownloadsViewController'? – user3480295
@ user3480295是的,我做过。 – He1nr1ch