swift - 为什么 Dispatch Group Notify 会被调用两次?

标签 swift firebase swift3 grand-central-dispatch

我试图让我的应用程序使用调度组来确保所有邀请都已发送,然后再继续。我认为只有在所有 enters 具有匹配的 leave 时才会调用 notify 回调,但我的似乎被多次调用,这是我的代码:

    for invite in invites {
        dispatchGroup.enter()
        let ref = FIRDatabase.database().reference().child("users").child(invite.id).child("invites")
        print(invite)
        ref.updateChildValues([name: nameTextField.text!]) { (error, ref) -> Void in
            dispatchGroup.leave()

            dispatchGroup.notify(queue: DispatchQueue.main, execute: {
                print("YOYOYO")
            })
        }
    }

在我的控制台中,我看到了 2 个“YOYOYO”,这让我很困惑。如果我做错了或者我的假设有误,有人可以告诉我吗?

最佳答案

您可能有两个邀请。如果你想在处理完所有 invites 后收到通知,请将 dispatchGroup.notify 移出 for 循环:

for invite in invites {
    dispatchGroup.enter()
    let ref = FIRDatabase.database().reference().child("users").child(invite.id).child("invites")
    print(invite)
    ref.updateChildValues([name: nameTextField.text!]) { (error, ref) -> Void in
        dispatchGroup.leave()            
    }
}

dispatchGroup.notify(queue: DispatchQueue.main) {
    print("YOYOYO")
}

关于swift - 为什么 Dispatch Group Notify 会被调用两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41867806/

相关文章:

ios - 在播放音频时保持 UIButton 处于选中状态

ios - 如何在 Swift3 中将 Any 转换为字典

ios - 我如何使用 swiftlint 强制每个 swift 文件必须包含指定的 header 值?

ios - 为什么图像没有存储在 Firebase 存储中并且 URL 为零?

java - 错误 :(26, 13) 无法解析 : com. google.firebase :firebase-database:11. 2.2

ios - 如何在 swift 3.0 中用 * 替换@之前的电子邮件 ID 文本

ios - 异常 'Invalid type in JSON write (__SwiftValue)' 而 JSON 使用数组编码 Swift 对象

ios - 通用 UITableView

ios - 用户移动时更新折线

c# - 如何在 Windows 上接收来自 Firebase 云消息传递的推送通知