У меня есть следующая функция, которая извлекает messages
из Firebase
, я хочу знать, когда Firebase
завершит загрузку всех дочерних узлов данных snapshot
, чтобы я мог передать сигнал через handler
что все данные завершили загрузку.
func loadLatestChatConversationMessages(forUserId forId: String!, handler: @escaping (Message?) -> ()){
guard let uid = Auth.auth().currentUser?.uid else {return}
let ref = REF_USERS_MESSAGES.child(uid).child(forId)
let firstBatchCount: Int = 12
RefUserMessagesChatConvoHandle = ref.queryOrderedByKey().queryLimited(toLast: UInt(firstBatchCount)).observe(.value, with: { (snapshot) in
if snapshot.childrenCount > 0 {
for child in snapshot.children.allObjects as! [DataSnapshot]{
let key = child.key
self.REF_MESSAGES.child(key).observeSingleEvent(of: .value, with: { (snapshot) in
guard var dictionary = snapshot.value as? [String : Any] else { return }
dictionary["messageId"] = key
let message = Message(dictionary: dictionary)
handler(message)
}, withCancel: nil)
}//end for
} else {
print("returning - no messages.")
handler(nil)
}// end if snapshot.childrenCount
}, withCancel: nil)
}//end func