Swift 串行调度块仅在委托后完成
Swift Serial Dispatch Block only finish after delegate
这很难解释。我正在创建一个串行队列来处理我的应用程序中的一些工作。想象一下,我做这样的事情:
dispatch_async(myQueue, { () -> Void in
self.SendSMS();
});
dispatch_async(myQueue, { () -> Void in
self.SendEmail();
});
现在我想做的是仅在委托(SendSMS 委托)完成其工作后调用 self.SendEmail。
有没有简单的方法可以做到这一点?
非常感谢
一种可行的方法是:
dispatch_async(myQueue, { () -> Void in
self.SendEmail();
});
在委托结束时。但我不知道这是不是唯一的方法。
干杯
是的,你可以做到,在接下来的步骤中:
// create tasks group handle
let taskGroup = dispatch_group_create()
let mainQueue = dispatch_get_main_queue()
// write your blocks in needed order
dispatch_group_async(taskGroup, mainQueue) { [weak self] in
// execute your code
// don't forget to use self with optional, i.e.: self!.property or function
self!.SendSMS()
}
dispatch_group_async(taskGroup, mainQueue) { [weak self] in
self!.SendEmail()
}
// and of course you need to catch completion of this task group
dispatch_group_notify(taskGroup, mainQueue) {
println("All work is done, milord!")
}
UPD. 上面的解决方案是关于没有顺序的异步执行,正如命名的那样,两者之一可以比声明的顺序更早完成。您需要使用依赖项作为持续执行的解决方案。我说的是多线程中的排序,而不是完成闭包或执行程序模式。
请注意,有不止一种情况可以执行此操作。其中之一——如下:
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) {
dispatch_sync(queue) {[weak self] in
self?.SendSMS()
}
dispatch_sync(queue) {[weak self] in
self?.SendEmail()
// here you need to call your completion of success function in main thread
}
}
请注意,函数中的代码必须存在于同一个队列中,并且对服务器请求使用同步方法。但这是另一个故事了;)
假设 SendSMS
是一个异步方法,我建议更改 SendSMS
以完成处理程序关闭:
// define property to hold closure
var smsCompletionHandler: (()->())?
// when you initiate the process, squirrel away the completion handler
func sendSMSWithCompletion(completion: (()->())?) {
smsCompletionHandler = completion
// initiate SMS
}
// when the SMS delegate method is called, call that completion closure
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
// do whatever you want when done
// finally, call completion handler and then release it
smsCompletionHandler?()
smsCompletionHandler = nil
}
因此,您可以这样称呼它,将 sendEmail
放在 sendSMS
的完成闭包中:
self.sendSMSWithCompletion() {
self.sendEmail()
}
我不知道你的 sendSMS
和 sendEmail
在做什么,但如果你调用 MessageUI
框架,你通常会在主队列上这样做.但是如果你真的需要在你的专用队列上执行上述操作,那么可以随意将它调度到那里。但希望这能说明这个概念:(a)供应完成处理程序关闭; (b) 保存它以便您的代表可以调用它;和 (c) 当委托被调用时,使用闭包 属性 然后重置它。
这很难解释。我正在创建一个串行队列来处理我的应用程序中的一些工作。想象一下,我做这样的事情:
dispatch_async(myQueue, { () -> Void in
self.SendSMS();
});
dispatch_async(myQueue, { () -> Void in
self.SendEmail();
});
现在我想做的是仅在委托(SendSMS 委托)完成其工作后调用 self.SendEmail。
有没有简单的方法可以做到这一点?
非常感谢
一种可行的方法是:
dispatch_async(myQueue, { () -> Void in
self.SendEmail();
});
在委托结束时。但我不知道这是不是唯一的方法。
干杯
是的,你可以做到,在接下来的步骤中:
// create tasks group handle
let taskGroup = dispatch_group_create()
let mainQueue = dispatch_get_main_queue()
// write your blocks in needed order
dispatch_group_async(taskGroup, mainQueue) { [weak self] in
// execute your code
// don't forget to use self with optional, i.e.: self!.property or function
self!.SendSMS()
}
dispatch_group_async(taskGroup, mainQueue) { [weak self] in
self!.SendEmail()
}
// and of course you need to catch completion of this task group
dispatch_group_notify(taskGroup, mainQueue) {
println("All work is done, milord!")
}
UPD. 上面的解决方案是关于没有顺序的异步执行,正如命名的那样,两者之一可以比声明的顺序更早完成。您需要使用依赖项作为持续执行的解决方案。我说的是多线程中的排序,而不是完成闭包或执行程序模式。 请注意,有不止一种情况可以执行此操作。其中之一——如下:
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) {
dispatch_sync(queue) {[weak self] in
self?.SendSMS()
}
dispatch_sync(queue) {[weak self] in
self?.SendEmail()
// here you need to call your completion of success function in main thread
}
}
请注意,函数中的代码必须存在于同一个队列中,并且对服务器请求使用同步方法。但这是另一个故事了;)
假设 SendSMS
是一个异步方法,我建议更改 SendSMS
以完成处理程序关闭:
// define property to hold closure
var smsCompletionHandler: (()->())?
// when you initiate the process, squirrel away the completion handler
func sendSMSWithCompletion(completion: (()->())?) {
smsCompletionHandler = completion
// initiate SMS
}
// when the SMS delegate method is called, call that completion closure
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
// do whatever you want when done
// finally, call completion handler and then release it
smsCompletionHandler?()
smsCompletionHandler = nil
}
因此,您可以这样称呼它,将 sendEmail
放在 sendSMS
的完成闭包中:
self.sendSMSWithCompletion() {
self.sendEmail()
}
我不知道你的 sendSMS
和 sendEmail
在做什么,但如果你调用 MessageUI
框架,你通常会在主队列上这样做.但是如果你真的需要在你的专用队列上执行上述操作,那么可以随意将它调度到那里。但希望这能说明这个概念:(a)供应完成处理程序关闭; (b) 保存它以便您的代表可以调用它;和 (c) 当委托被调用时,使用闭包 属性 然后重置它。