简体   繁体   English

我可以区分“DispatchGroup”异步任务完成吗?

[英]Can I distinguish `DispatchGroup` async tasks completion?

Suppose I have three async functions A, B, C.假设我有三个异步函数 A、B、C。

I want a callback after completion of these three, I will use DispatchGroup .我想在完成这三个后回调,我将使用DispatchGroup

But I have a batch of these async tasks A, B, C.但我有一批这些异步任务 A、B、C。 Suppose, 6 of them.假设,其中 6 个。

Now I want a callback after each of these A, B, C async task completion.现在我想要在每个 A、B、C 异步任务完成后进行回调。

Like,喜欢,

A
B
C
Callback
A
B
C
Callback
A
B
C
Callback

Using DispatchGroup , I am getting,使用DispatchGroup ,我得到,

A
B
C
A
B
C
A
B
C
Callback

as group,notify() works only when everyone have group.leave() .作为group,notify()仅在每个人都有group.leave()

Sample,样本,

// Called 6 times

private func fetchLists(in room: Room, 
completion: @escaping(                                                                   _ ownerList: [Any], 
_ memberList: [Any],
_ subscribersList: [Any]) -> Void) {

        var ownerList = [Any]()
        var memberList = [Any]()
        var subscribersList = [Any]()
        
        let group = DispatchGroup()

        group.enter()
        // Asyncronous
        fetchOwners(in: room, completion: { list in
            ownerList = list
            group.leave()
        })
        
        group.enter()
        // Asyncronous
        fetchMembers(in: room, completion: { list in
            memberList = list
            group.leave()
        })
        
        group.enter()
        // Asyncronous
        fetchSubscribers(in: room, completion: { list in
            subscribersList = list
            group.leave()
        })
    
        debug("Called 6 times")
        group.notify(queue: .main) {
            debug("Called 1 time. Want it to be called 6 times each with 3 batch.")
            completion(memberList, ownerList, subscribersList)
        }
    }

As per DispatchGroup 's documentation the notify method is called when group is empty, which means you have to call group.leave() as many times as you have called group.enter() .根据DispatchGroup文档,当 group 为空时调用 notify 方法,这意味着您必须调用group.enter() () 与调用group.leave()一样多次。 For your scenario, since you want to notify group each time a batch operation completes you have to use a separate group for each batch of tasks.对于您的方案,由于您希望在每次批处理操作完成时通知组,您必须为每批任务使用单独的组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在DispatchGroup中执行完成处理程序 - Execute Completion Handler in DispatchGroup 我可以实例化一个 ViewController,并在继续之前等待几个 ViewController 完成任务吗? - Can I instantiate a ViewController, and await completion of tasks a few ViewControllers down the line before continuing? 如何在后台会话中区分下载任务? - How do I distinguish between download tasks in a background session? 使用 dispatchgroup 在 for 循环中等待任务完成,如果失败则退出循环 - using dispatchgroup wait for task completion in for loop and exit from loop if failure 如何使用DispatchGroup查找照片并将其存储在UIImage数组中 - How can I use DispatchGroup to look up photos and store in UIImage array 如何本地避免多个异步任务的嵌套块,这些异步任务必须在另一任务完成后执行 - How to natively avoid the nesting block for multiple async tasks which must be executed one after the completion of another 无法使DispatchGroup在Swift中正常工作 - Can't get DispatchGroup to work properly in Swift 在Swift中使用dispatchGroup处理一系列异步函数时出错 - Error in handling a series of async functions using dispatchGroup in Swift Swift 4 异步调用 for 循环使用 DispatchGroup、DispatchQueue 和 DispatchSemaphore 按顺序执行 - Swift 4 async call with for loop execute in order using DispatchGroup, DispatchQueue and DispatchSemaphore Firestore:使用SnapshotListener调用异步函数,并在DispatchGroup循环中导致崩溃 - Firestore: Calls with of async function with SnapshotListener and in a cycle with DispatchGroup cause a crash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM