ios - scheduling one task after another BUT with sessions on board Swift -


trying understand how can schedule 1 task behind other, looked @ gcd , nsoperations, both seem @ abstraction far removed core code; part of executes on own thread!

i tried code obvious find...

    let date = nsdate()     print("getting links \(date)")      let operationqueue: nsoperationqueue = nsoperationqueue.mainqueue()     let completionblockoperation: nsblockoperation = nsblockoperation.init(         block: {             self.reportfini()         }     )      let workerblockoperation:nsblockoperation = nsblockoperation.init(         block: {             self.getlinks()         }     )     completionblockoperation.adddependency(workerblockoperation)     operationqueue.addoperation(workerblockoperation)     operationqueue.addoperation(completionblockoperation) 

now reportfini next nothing ...

func reportfini() {     let date = nsdate()     print("got links \(date)") } 

but getlinks more complex, using sessions... in nutshell runs

    let request = nsmutableurlrequest(url: nsurl(string: "https://blah")!)     let session = nsurlsession.sharedsession()     request.httpmethod = "post"      request.addvalue("application/json",forhttpheaderfield: "content-type")     request.addvalue("path", forhttpheaderfield: lepath)     request.addvalue("settings", forhttpheaderfield: "requested_visibility\": \"public\"}")      var nodea:nsmutabledictionary? = ["path":lepath]     let nodeb:nsmutabledictionary? = ["requested_visibility":"public"]     nodea!.setvalue(nodeb, forkey: "settings")      {         let jsondata = try nsjsonserialization.datawithjsonobject(nodea!, options: [])         request.httpbody = jsondata     } catch {         completion(string: nil, error: error)     }     var string2return = ""     var stringpath = ""     let task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in         if let error = error {             completion(string: nil, error: error)             return         }         let strdata = nsstring(data: data!, encoding: nsutf8stringencoding)         print("body: \(strdata)\n\n")         {             let jsonresult = try nsjsonserialization.jsonobjectwithdata(data!, options:nsjsonreadingoptions.mutablecontainers);             self.jsonparser(jsonresult,field2file: "ignore")              if let stringpath = (self.parsedjson["url"] as? string) {                 string2return = stringpath             } else {                 string2return = (self.parsedjson["error_summary"] as? string)!             }             completion(string: string2return, error: error)         } catch {             completion(string: nil, error: error)         }     })     task.resume() 

}

now, getlinks , reportfini both execute @ same time; links goes off on own thread! , returns... need/want notified when finished, cause got else do.

it more complicated, since need run 10 sessions (fetching 10 links) concurrently, in parallel; , want notified when they're finished.

--- update ---

tried gcd enclosing sessions code in

    let workerqueue = dispatch_queue_create("getlinks", dispatch_queue_concurrent)     let getlinksgroup = dispatch_group_create()      dispatch_group_notify(getlinksgroup, dispatch_get_main_queue()) {         print("all links downloaded")     }      dispatch_group_enter(getlinksgroup)     dispatch_group_async(getlinksgroup, workerqueue) { 

exiting ...

    dispatch_group_leave(getlinksgroup) 

unfortunately doesn't work; had seen sessions launch own thread , notified code has completed, before finishes url data download tasks.

going try kvo ...

managed solve this; using gcd & kvo, not manage sessions; manage counter, , throw trigger changing monitored kvo value. solution, tell me.

public class synchronizedint<t> { private var blob:int = 0 private let accessqueue = dispatch_queue_create("synchronizedintaccess", dispatch_queue_serial)  public func pending(queue2go:t)  {     dispatch_sync(self.accessqueue) {         self.blob = queue2go as! int     } }  public func fulfilled() -> bool {     dispatch_sync(self.accessqueue) {         self.blob = self.blob - 1     }     if self.blob == 0 {         return true     } else {         return false     } }  public func copy() -> int {     return self.blob }  } 

which using keep track of sessions launched, can notified when complete. completion track in completion blocks, each of checks see if final one. beyond used kvo of sorts based on first swifty solution presented here.

http://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -