在实例化对象

时间:2017-04-19 18:39:16

标签: firebase swift3 firebase-realtime-database grand-central-dispatch firebase-storage

我想从firebase下载:

  • 从组配置文件发出的数据(Firebase实时数据库) 包含...
    • 从组管理员资料(Firebase实时数据库)发布的数据
    • 群组资料图片(Firebase存储)

然后我可以使用其数据及其图像实例化一个组对象

第一种方法,我使用了3个嵌套的闭包,这些闭包允许我获取数据,然后获取图像。

它确实有效,但从firebase顺序获取所有内容的时间很长。

所以我尝试使用GCD同时推送我的2个最新Firebase查询(用户数据+组图像)(而不是一个接一个),并等待最后一个回调开始实例化我的组

这是一种正确的方法吗?

如果是,我发现实施它有些困难...... 我的问题:returnedUser和returnedGroupImage总是为nil

这是我的一堆代码:

static func getGroup(_ groupID:String, completionBlock: @escaping (_ group: Group?) -> ()) { 
   dataRef.child("data").child("groups").child(groupID).observe(.value, with: { (snapshot) in  
        if let snapshotValue = snapshot.value {
            guard let name = (snapshotValue as AnyObject).object(forKey: "name") as? String else
            {
                completionBlock(nil)
                return
            }

            guard let adminID = (snapshotValue as AnyObject).object(forKey: "adminID") as? String else
            {
                completionBlock(nil)
                return
            }


            let queue = DispatchQueue(label: "asyncQueue", attributes: .concurrent, target: .main)
            let dispatch_group = DispatchGroup()


            var returnedUser: User?
            var returnedGroupImage: UIImage?


            queue.async (group: dispatch_group) {
            FireBaseHelper.getUser(adminID, completionBlock: { (user) in
                if user != nil {
                    returnedUser = user
                }
            })
            }

            queue.async (group: dispatch_group) {
            FireBaseHelper.getGroupImage(groupID, completionBlock: { (image) in
                if image != nil {
                    returnedGroupImage = image
                }
            })
            }

            dispatch_group.notify(queue: DispatchQueue.main) {
                // Single callback that is supposed to be executed after all tasks are complete.
                if (returnedUser == nil) || (returnedGroupImage == nil) {
                    // always true !
                    return
                }

                let returnedGroup = Group(knownID: (snapshotValue as AnyObject).key, named: name, createdByUser: currentUser!)

                returnedGroup.groupImage = returnedGroupImage
                completionBlock(returnedGroup)
            }
        }
    })
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我相信您使用DispatchGroup的方式不正确。

let dispatch_group = DispatchGroup()

var returnedUser: User?
var returnedGroupImage: UIImage?

dispatch_group.enter()
FireBaseHelper.getUser(adminID, completionBlock: { (user) in
   if user != nil {
      returnedUser = user
   }
   dispatch_group.leave()
})

dispatch_group.enter()
FireBaseHelper.getGroupImage(groupID, completionBlock: { (image) in
   if image != nil {
      returnedGroupImage = image
   }
   dispatch_group.leave()
})

dispatch_group.notify(queue: DispatchQueue.main) {
// Single callback that is supposed to be executed after all tasks are complete.
   if (returnedUser == nil) || (returnedGroupImage == nil) {
      // always true !
      return
   }

   let returnedGroup = Group(knownID: (snapshotValue as AnyObject).key, named: name, createdByUser: currentUser!)

   returnedGroup.groupImage = returnedGroupImage
   completionBlock(returnedGroup)
}