嵌套循环的调度组 [英] Dispatch Group for nested loop

查看:63
本文介绍了嵌套循环的调度组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与多个用户列表共享扩展名,并且可以为所有选定的用户选择多个用户和多个图像.为此,我使用了调度组,并为所有用户提供了两个循环1,并为所有选定的图像提供了两个循环.

I've share extension with multiple user list and I can select many user and multiple image to all selected user. For that I used dispatch group and there are two loop 1 for all users and inside that loop for all selected images.

let myGroup = DispatchGroup()
let myGroupMain = DispatchGroup()
let dispatchQueue = DispatchQueue.global(qos: .background)
dispatchQueue.async {
for indexPath in self.indexPathContain  {
 myGroupMain.enter()

 for (index, attachment) in (content.attachments as! [NSItemProvider]).enumerated() {
   myGroup.enter()
  let paramDict = [
   "id":"0",
   "chat_id":modelList.id!,
   "sender_id":  x,
   "image":"",
   "receiver_id": modelList.sender_id!,
   "type": "1",
   "message": "",
   "file":dataArr,
   ] as [String : Any]
   self?.socket?.emit("upload_files", paramDict)
    myGroup.leave()
 }
 myGroup.notify(queue: .main) {
            myGroupMain.leave()
        }
}
 myGroupMain.notify(queue: .main) {

       self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
                print(i)
           }

}

在上述情况下,当我执行时,它会 myGroupMain.notify ,而不会完成 myGroup 循环.Swift 5/Xcode 11.3

In above case when I execute then it will myGroupMain.notify without complete myGroup loop completion. Swift 5/Xcode 11.3

推荐答案

以前,我也遇到过同样的问题.我通过全局定义 myGroup myGroupMain 来解决此问题.

Earlier, I have faced the same issue. I fixed the issue by defining myGroup and myGroupMain globally.

请参考以下代码.

class ClassName {

    let myGroup = DispatchGroup()
    let myGroupMain = DispatchGroup()
    let dispatchQueue = DispatchQueue.global(qos: .background)

    func yourCode(){
        dispatchQueue.async {
            for indexPath in self.indexPathContain  {
                myGroupMain.enter()

                for (index, attachment) in (content.attachments as! [NSItemProvider]).enumerated() {
                    myGroup.enter()
                    let paramDict = [
                        "id":"0",
                        "chat_id":modelList.id!,
                        "sender_id":  x,
                        "image":"",
                        "receiver_id": modelList.sender_id!,
                        "type": "1",
                        "message": "",
                        "file":dataArr,
                        ] as [String : Any]
                    self?.socket?.emit("upload_files", paramDict)
                    myGroup.leave()
                }
                myGroup.notify(queue: .main) {
                    myGroupMain.leave()
                }
            }
            myGroupMain.notify(queue: .main) {

                self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
                print(i)
            }

        }
}

这篇关于嵌套循环的调度组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆