[英]DispatchGroup notify before all leave called
I have an iOS application that runs multiple classes(every class in a separate thread) and I want to get a notification when all the class finishes their run.我有一个运行多个类的 iOS 应用程序(每个类都在一个单独的线程中),我想在所有类完成运行时收到通知。
So I have a BaseClass:所以我有一个基类:
class BaseClass {
var completeState: CompleteState
init(completeState: CompleteState) {
self.completeState = completeState
self.completeState.dispatch.enter()
}
func start() { }
}
And this is the CompleteState Class:这是 CompleteState 类:
class CompleteState {
let userId:String
let dispatch = DispatchGroup()
init(userId:String) {
self.userId = userId
dispatch.notify(queue: DispatchQueue.main) {
print("Finish load all data")
}
}
}
And I have multiple classes that inherit from BaseClass
, and all of them are in this form:我有多个继承自
BaseClass
的类,它们都是这种形式:
class ExampleClass1: BaseClass {
override func start() {
DispatchQueue.global().async {
print("ExampleClass1 - Start running")
//DOING SOME CALCULATIONS
print("ExampleClass1 - Finish running")
self.completeState.dispatch.leave()
}
}
}
This is the code that runs all of this:这是运行所有这些的代码:
public class RunAll {
let completeState: CompleteState
let classArray: [BaseClass]
public init(userId: String) {
self.completeState = CompleteState(userId: userId)
classArray = [ExampleClass1(completeState: completeState),
ExampleClass2(completeState: completeState),
ExampleClass3(completeState: completeState),
ExampleClass4(completeState: completeState),
ExampleClass5(completeState: completeState),
ExampleClass6(completeState: completeState),
ExampleClass7(completeState: completeState),
ExampleClass8(completeState: completeState),
ExampleClass9(completeState: completeState)]
startClasses()
}
private func startClasses() {
for foo in classArray {
foo.start()
}
}
}
And the problem is that I get dispatch.notify
called before all the classes finish their work, any idea what is the problem?问题是在所有类完成工作之前我得到了
dispatch.notify
调用,知道是什么问题吗?
The documentation for notify
states: notify
状态的文档:
This function schedules a notification block to be submitted to the specified queue when all blocks associated with the dispatch group have completed.
当与调度组关联的所有块都完成时,此函数安排一个通知块提交到指定队列。 If the group is empty (no block objects are associated with the dispatch group), the notification block object is submitted immediately.
如果该组为空(没有块对象与调度组相关联),则立即提交通知块对象。 When the notification block is submitted, the group is empty.
提交通知块时,该组为空。
You are calling dispatch.notify()
in your CompleteState
init
.您在
CompleteState
init
中调用dispatch.notify()
。 At the time you are calling notify
, the dispatch group is empty, because you haven't yet created a BaseClass
subclass let along called start
, which is where the dispatch group is entered.在您调用
notify
时,调度组是空的,因为您还没有创建一个名为start
的BaseClass
子类,这是进入调度组的地方。
Because you call notify
when the dispatch group is empty, the block is submitted immediately.因为你在dispatch group为空的时候调用了
notify
,所以block是立刻提交的。
It may be worth looking into OperationQueue
, but if you want to use what you have you can split the notify
out from the init
:可能值得研究
OperationQueue
,但如果您想使用您拥有的东西,您可以将notify
从init
中分离出来:
lass CompleteState {
let userId:String
let dispatch = DispatchGroup()
init(userId:String) {
self.userId = userId
}
func registerCompletionBlock(handler: @escaping () -> Void) {
self.dispatch.notify(queue: DispatchQueue.main, execute: handler)
}
}
You would then provide the completion block after you call startClasses
然后,您将在调用
startClasses
后提供完成块
public init(userId: String) {
self.completeState = CompleteState(userId: userId)
classArray = [ExampleClass1(completeState: completeState),
ExampleClass2(completeState: completeState),
ExampleClass3(completeState: completeState),
ExampleClass4(completeState: completeState),
ExampleClass5(completeState: completeState),
ExampleClass6(completeState: completeState),
ExampleClass7(completeState: completeState),
ExampleClass8(completeState: completeState),
ExampleClass9(completeState: completeState)]
startClasses()
self.completeState.registerCompletionBlockHandler {
print("Finish load all data")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.