ios - Swift Serial Dispatch Block only finish after delegate -
ios - Swift Serial Dispatch Block only finish after delegate -
this hard 1 explain. creating serial queue handling work in app. imagine this:
dispatch_async(myqueue, { () -> void in self.sendsms(); }); dispatch_async(myqueue, { () -> void in self.sendemail(); }); now phone call self.sendemail after delegate(sendsms delegate) finishes work.
is there simple way this?
many thanks
assuming sendsms asynchronous method, i'd advise changing sendsms take completion handler closure:
// define property hold closure var smscompletionhandler: (()->())? // when initiate process, squirrel away completion handler func sendsmswithcompletion(completion: (()->())?) { smscompletionhandler = completion // initiate sms } // when sms delegate method called, phone call completion closure func messagecomposeviewcontroller(controller: mfmessagecomposeviewcontroller!, didfinishwithresult result: messagecomposeresult) { // whatever want when done // finally, phone call completion handler , release smscompletionhandler?() smscompletionhandler = nil } thus, you'd phone call so, putting sendemail within completion closure of sendsms:
self.sendsmswithcompletion() { self.sendemail() } i don't know sendsms , sendemail doing, if you're calling messageui framework, you'd on main queue. if need above on dedicated queue, sense free dispatch there. illustrates concept: (a) supply completion handler closure; (b) save delegate can phone call it; , (c) when delegate called, utilize closure property , reset it.
ios swift asynchronous
Comments
Post a Comment