关闭时下载停止 ViewController
Download Stops When Dismissing ViewController
我在 Swift 中使用 Cheapjack Library (iOS) 下载我的应用程序中的文件。我从另一个 class 排队下载。这似乎工作正常,下载排队并开始下载。但是,当下载发生的 "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。
请注意 CheapjackManager
的 delegate
是 weak
。关闭 DownloadsViewController
后,delegate
将设置为 nil,对 delegate
的任何调用都将被忽略,包括 cheapjackManager:didiFinishDownload:
.
同时,CheapjackManager
还在为您下载资源。只是无法在 DownloadsViewController
.
中观察到它
我在 Swift 中使用 Cheapjack Library (iOS) 下载我的应用程序中的文件。我从另一个 class 排队下载。这似乎工作正常,下载排队并开始下载。但是,当下载发生的 "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。
请注意 CheapjackManager
的 delegate
是 weak
。关闭 DownloadsViewController
后,delegate
将设置为 nil,对 delegate
的任何调用都将被忽略,包括 cheapjackManager:didiFinishDownload:
.
同时,CheapjackManager
还在为您下载资源。只是无法在 DownloadsViewController
.