私はCheapjack Library (iOS)を使用して、Swiftのアプリケーション内のファイルをダウンロードしています。他のクラスからダウンロードをキューに入れます。これは正常に動作するように見える、ダウンロードがキューに入れられ、ダウンロードを開始します。ただし、ダウンロードが行われる「DownloadsViewController」が表示されていない場合、ファイルのダウンロードが完了すると、didiFinishDownloadブロックは実行されません。どのように私のエラーを処理することができるかに関する任意のアイデア。 また、私はダウンロードViewControllerを開き、それを閉じると、ダウンロードは、私が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")
}
}
}
CheapjackManager.sharedManagerの代理人を 'DownloadsViewController'に設定しましたか? – user3480295
@ user3480295はい、私はしました。 – He1nr1ch