ios - Why is the result of the code same? The code is using only the value of the last function for all the looping -


all value in variable (filename, destinationfolder) different loop except "let uploaddidfinish: (()-> void)". learnt, supposed different declared in different instance.

i put following code in view controller viewdidload()

let uploaddidfinish: ()-> void = {     print("\nunzip \(self.foldername+".zip")\n")     self.c.unzipfile(foldername: self.mfoldername+"/"+self.foldername,filename: self.foldername+".zip") } downloadmanager.shared.downloadfile("https://ping.passivealtitude.com/magazinefile/"+foldername+"/"+foldername+".zip", to: mfoldername+"/"+foldername, filename: foldername+".zip", upload: uploaddidfinish) 

after loop 4 times, result of code above

downloaded file:///users/vivian/library/developer/coresimulator/devices/3ff96128-fc8f-4d44-9b4d-dc1989eff9ff/data/containers/data/application/15b32c21-6117-4cd9-a70e-7c0bfc0dfa6a/documents/magazine/book1/book1.zip

downloaded file:///users/vivian/library/developer/coresimulator/devices/3ff96128-fc8f-4d44-9b4d-dc1989eff9ff/data/containers/data/application/15b32c21-6117-4cd9-a70e-7c0bfc0dfa6a/documents/magazine/book2/book2.zip

downloaded file:///users/vivian/library/developer/coresimulator/devices/3ff96128-fc8f-4d44-9b4d-dc1989eff9ff/data/containers/data/application/15b32c21-6117-4cd9-a70e-7c0bfc0dfa6a/documents/magazine/book3/book3.zip

downloaded file:///users/vivian/library/developer/coresimulator/devices/3ff96128-fc8f-4d44-9b4d-dc1989eff9ff/data/containers/data/application/15b32c21-6117-4cd9-a70e-7c0bfc0dfa6a/documents/magazine/book4/book4.zip

unzip book4.zip

unzip book4.zip

unzip book4.zip

unzip book4.zip

when supposed

unzip book1.zip

unzip book2.zip

unzip book3.zip

unzip book4.zip

first function passed

class downloadmanager: nsobject { ...  @discardableresult func downloadfile(_ url: string, foldername: string, filename:string, upload: @escaping (()->void)) -> downloadoperation {     let link = url(string: url)     let destinationfolder = createdirectory(foldername: foldername)     let operation = downloadoperation(session: session, url: link!, destinationfolder: destinationfolder, filename:filename, upload: upload)     operations[operation.task.taskidentifier] = operation     queue.addoperation(operation)     return operation } 

then, have passed download file function

class downloadoperation : asynchronousoperation {     var task: urlsessiontask!     let destinationfolder: url     let filename: string     let uploaddidfinish: (()-> void)     let manager = filemanager.default      init(session: urlsession, url: url, destinationfolder: url,filename: string, upload: @escaping (()->void)) {         self.filename = filename         self.destinationfolder = destinationfolder         self.uploaddidfinish = upload         super.init()         task = session.downloadtask(with: url)     }      override func cancel() {         task.cancel()         super.cancel()     }      override func main() {         task.resume()     } }  extension downloadoperation: urlsessiondownloaddelegate {      func urlsession(_ session: urlsession, downloadtask: urlsessiondownloadtask, didfinishdownloadingto location: url) {         {             let destinationurl = destinationfolder.appendingpathcomponent(filename)             if manager.fileexists(atpath: destinationurl.path) {                 try manager.removeitem(at: destinationurl)             }             try manager.moveitem(at: location, to: destinationurl)             print(" \ndownloaded \(destinationurl)\n")         } catch {             print("downloadoperation: urlsessiondownloaddelegate: \(error)")         }          self.uploaddidfinish()      } 

the code below connects view controller download manager.

dispatchqueue.main.async {             self.uploaddidfinish()         } 

the original code how download multiple files sequentially using nsurlsession downloadtask in swift

why result same? appreciated!

when uploaddidfinish been execute self.foldername changed book4, print("\nunzip \(self.foldername+".zip")\n") output: unzip book4.zip. e.g.

class downloadoperation : asynchronousoperation {     ...         let uploaddidfinish: ((string, string)-> void)     init(session: urlsession, url: url, destinationfolder: url,filename: string, upload: @escaping ((string, string)->void)) {         ...                self.uploaddidfinish = upload     } }   extension downloadoperation: urlsessiondownloaddelegate {     func urlsession(_ session: urlsession, downloadtask: urlsessiondownloadtask, didfinishdownloadingto location: url) {         ...         self.uploaddidfinish(filename, destinationfolder.path)     } } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -