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