Nested Dispatch Groups Swift

淺唱寂寞╮ 提交于 2019-12-04 06:44:53

This is a very good approach for async task tracking.

It is important to check if all code paths are covered with leave(). You have a potential bug, if the if let doesn't have the value not optional. Fixed here:

func createGroup(onSccess completion:@escaping () -> Void) {
    [...]
    // store picture
    dataDispatchGroup.enter()
    let storageRef = FIRStorage.storage().reference().child("profile_images").child("\(name).png")
    if let uploadData = UIImagePNGRepresentation(profImage.resizeImage(targetSize: CGSize(width: 500, height: Int(500*(profImage.size.height/profImage.size.width))))) {
        storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in
            dataDispatchGroup.leave()
        })
    } else {
        dataDispatchGroup.leave()
    }
    [...]
}

In general always also make sure that all completion blocks are called in all of the code paths of methods you use. There always may be a bug in Firebase or there may be something about the completion not being called in the documentation of those methods. But again, this is the way to go.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!