0

之前,我想從火力下載:從組配置文件發出等待2個回調實例化對象

  • 數據(火力地堡實時DB) 包括...從發行
    • 數據組管理輪廓(火力地堡實時DB)
    • 一組配置文件圖像(火力地堡存儲)

然後我可以實例化其數據的一組對象及其圖像

第一方法中,我使用的3嵌套閉合,讓我獲取數據,然後,以獲得圖像。

它確實有效,但從Firebase獲取所有這些東西的時間相當長。

因此,我嘗試使用GCD,以便在同一時間(而不是一個接一個地)推送我最近的2個Firebase查詢(用戶數據+組圖像),並等待最後一個回調開始實例化我的組。

這是一個正確的方法嗎?

如果是的話,我覺得有些困難來實現它... 我的問題:returnedUser和returnedGroupImage總是零

這裏是我的一堆代碼:

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) 
      } 
     } 
    }) 
} 

感謝您的幫助!

+0

'FireBaseHelper.getUser'和'FireBaseHelper.getGroupImage'是異步函數。我相信你在這裏使用'DispatchGroup'的方式看起來不正確。 – KrishnaCA

回答

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) 
} 
+0

非常感謝Krishna!有用 !! –