2016-07-23 57 views
0

我正在使用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") 
      } 
     } 
} 
+0

您是否將CheapjackManager.sharedManager的代理設置爲'DownloadsViewController'? – user3480295

+0

@ user3480295是的,我做過。 – He1nr1ch

回答

1

DownloadsViewController將DEINIT。

請注意,delegateCheapjackManagerweak。在您解僱DownloadsViewController後,delegate將被設置爲零,任何呼叫delegate都將被忽略,包括cheapjackManager:didiFinishDownload:

與此同時,CheapjackManager仍在爲您下載資源。只是不能在DownloadsViewController中觀察它。

+0

我可以避免deinit調用嗎? – He1nr1ch

+0

嘗試將委託設置爲某個其他實例,例如'AppDelegate'。 – user3480295

+0

非常感謝你,似乎在伎倆。 – He1nr1ch