Ios 在swift 3中下载文件最多只能完成24KB?使用URLSession

Ios 在swift 3中下载文件最多只能完成24KB?使用URLSession,ios,swift,nsurlsession,nsurlsessiondownloadtask,Ios,Swift,Nsurlsession,Nsurlsessiondownloadtask,我用它来下载swift 3中的文件。我试图通过添加一个特定的循环来操作它,以便能够下载n个文件。它可以工作,但只完成24KB 这是我的密码: for jsonDict in data{ var jasonData = Attachment() if let data = (jsonDict["url"] as? String) {jasonData.url = data} else { jasonData.url = ""} self.beginDownloadFile(jaso

我用它来下载swift 3中的文件。我试图通过添加一个特定的循环来操作它,以便能够下载n个文件。它可以工作,但只完成24KB

这是我的密码:

for jsonDict in data{
  var jasonData = Attachment()

  if let data = (jsonDict["url"] as? String) {jasonData.url = data} else { jasonData.url = ""}

  self.beginDownloadFile(jasonData: jasonData)
}

func beginDownloadFile(jasonData: Attachment){
        let identifier = jasonData.customer_id + "/attachment/" + jasonData.patient_guid + "@%E%@" + "/" + jasonData.filename + "@%E%@" + jasonData.customer_id  + "@%E%@" + jasonData.patient_guid

        let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)

        backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)

        var link = self.constants.PATIENTS_PICTURE_LINK + jasonData.customer_id + "/attachment/" + jasonData.guid + "/" + jasonData.filename
        link = link.replacingOccurrences(of: " ", with: "%20")

        let url = URL(string: link)!

        downloadTask = backgroundSession.downloadTask(with: url)
        downloadTask.resume()
    }

    func urlSession(_ session: URLSession,
                    downloadTask: URLSessionDownloadTask,
                    didFinishDownloadingTo location: URL){
        let string_container = session.configuration.identifier!.components(separatedBy: "@%E%@")

        let folderPath = string_container[0]
        let fileName = string_container[1]
        let customerID = string_container[2]
        let patientGUID = string_container[3]

        print("GUID IS: " + patientGUID)

        let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let documentDirectoryPath:String = path[0]

        let fileManager = FileManager()

        let path1 = documentDirectoryPath.stringByAppendingPathComponent(pathComponent: customerID)
        let path2 = path1.stringByAppendingPathComponent(pathComponent: "attachment")
        let path3 = path2.stringByAppendingPathComponent(pathComponent: patientGUID)

        let destinationURLForFile = URL(fileURLWithPath: path3.appendingFormat(fileName))

        do {
            if !fileManager.fileExists(atPath: path1) {
                try fileManager.createDirectory(atPath: path1, withIntermediateDirectories: false, attributes: nil)
            }
            if !fileManager.fileExists(atPath: path2) {
                try fileManager.createDirectory(atPath: path2, withIntermediateDirectories: false, attributes: nil)
            }

            if !fileManager.fileExists(atPath: path3) {
                try fileManager.createDirectory(atPath: path3, withIntermediateDirectories: false, attributes: nil)
            }

        } catch let error as NSError {
            print("ATTACHMENT ERROR: " + error.localizedDescription);
        }

        if fileManager.fileExists(atPath: destinationURLForFile.path){
            //showFileWithPath(path: destinationURLForFile.path)
            do {
                try fileManager.removeItem(at: destinationURLForFile)
                try fileManager.moveItem(at: location, to: destinationURLForFile)
                //showFileWithPath(path: destinationURLForFile.path)
            }catch{
                print("An error occurred while moving/deleting file to destination url")
            }
        }
        else{
            do {
                try fileManager.moveItem(at: location, to: destinationURLForFile)
                //showFileWithPath(path: destinationURLForFile.path)
            }catch{
                print("An error occurred while moving file to destination url")
            }
        }
    }
结果:


是什么导致它停止@24KB?是因为循环吗?或者标识符操作?

您在哪里声明下载任务的代码?它是实例属性吗

如果是这样,那么每次循环运行时,您都会扔掉指向
downloadTask
的指针,然后运行此代码:

downloadTask=backgroundSession.downloadTask(带:url)
downloadTask.resume()

您的任务可能因此提前发布。如果是这样,那么最后一次通过循环下载应该完成


另外,24k是否可能是正确的文件大小?或者静态错误页面的大小?

会话将任务保留在其中。然而,假设我有限的Swift知识没有误解这里发生的事情,问题是代码没有保留会话。嗨!很抱歉回复太晚,我只是在早些时候恢复了编码。这似乎是一个错误页面。我重新检查了我的链接,它的guid链接部分似乎有问题。虽然它在工作,但我的urlsession做得对吗?我将文件路径生成所需的数据放入urlsession标识符中。