我有以下函数,该函数从Firebase提取数据,填充compliments
数组,并假设通过完成handler
将数组传递回来。
即使将对象添加到数组中,返回的数组始终为空?
我是否已将完成处理程序代码放置在错误的位置,我尝试在所有花括号中插入并且没有任何效果。
执行行handler(compliments, true)
上的断点将输出以下内容:
0x0000000102bb3fe8 vipeeps`partial apply forwarder for closure #1 (Swift.Array<Any>, Swift.Bool) -> () in vipeeps.ConversationsListVC.loadNewInvitesData() -> () at ConversationsListVC.swift
功能:
func getComplimentsReceived(forUserId forId: String, handler: @escaping (_ complimentPack: [[String : Any]] ,_ success: Bool) -> ()){
var compliments = [[String : Any]]()//empty array to hold compliments
REF_USERS_COMPLIMENTS.child(forId).observe(.value) { (snapshot) in
for item in snapshot.children{
let itemSnap = item as! DataSnapshot
let dict = itemSnap.value as! [String : Bool]
for (key, status) in dict {
switch status {
case true:
//print(status)
self.REF_USERS.child(snapshot.key).observeSingleEvent(of: .value, with: { (snapshot) in
let uid = snapshot.key
let name = snapshot.childSnapshot(forPath: "name").value as! String
let email = snapshot.childSnapshot(forPath: "email").value as! String
let profilePictureURL = snapshot.childSnapshot(forPath: "profilePictureURL").value as! String
let birthday = snapshot.childSnapshot(forPath: "birthday").value as! String
let firstName = snapshot.childSnapshot(forPath: "firstName").value as! String
let lastName = snapshot.childSnapshot(forPath: "lastName").value as! String
let gender = snapshot.childSnapshot(forPath: "gender").value as! String
let discoverable = snapshot.childSnapshot(forPath: "discoverable").value as! Bool
let online = snapshot.childSnapshot(forPath: "online").value as! Bool
let discoveryPrefs = snapshot.childSnapshot(forPath: "discoveryPrefs").value as! [String : Any]
let dictionary: [String : Any] = ["uid": uid, "name": name, "email": email, "profilePictureURL": profilePictureURL, "birthday": birthday, "firstName": firstName, "lastName": lastName, "gender": gender, "discoverable": discoverable, "online": online, "discoveryPrefs": discoveryPrefs]
let user = User(uid: uid, dictionary: dictionary)
let complimentPack = [
"type": "compliment",
"complimentId": key,
"active": status,
"fromUser": user
] as [String : Any]
compliments.append(complimentPack)
print("compliments.count: \(compliments.count)")
})//end observer
case false:
print(status)
break
}//end switch
}//end for dict
}//end for snapshot.item
handler(compliments, true)
}//end observe
}//end func
数据结构:
答案 0 :(得分:1)
您可能想使用调度组,所有异步observeSingelEvent
调用完成后,调度组将被通知:
observe
代码中,创建一个新的DispatchGroup
observeSingleEvent
之前,请先enter
组。leave
将该组enters
和leave
调用都匹配,就会调用notify
,然后调用handler
查看此处:
func getComplimentsReceived(forUserId forId: String, handler: @escaping (_ complimentPack: [Any] ,_ success: Bool) -> ()){
REF_USERS_COMPLIMENTS.child(forId).observe(.value) { (snapshot) in
var compliments = [Any]()//empty array to hold compliments
let dispatchGroup = DispatchGroup()
for item in snapshot.children {
for (key, status) in dict {
switch status {
case true:
dispatchGroup.enter()
self.REF_USERS.child(snapshot.key).observeSingleEvent(of: .value, with: { (snapshot) in
// ...
compliments.append(complimentPack)
dispatchGroup.leave()
})//end observer
case false:
print(status)
break
}//end switch
}//end for dict
} //end for snapshot.item
dispatchGroup.notify(queue: .main) {
handler(compliments, true)
}
} //end observe
}//end func
添加编辑功能(通过@Roggie):
func loadNewInvitesData(){
guard let uid = Auth.auth().currentUser?.uid else { return }
DataService.run.getInvitesAndCompliments(forUserId: uid) { (array, sucess) in
for item in array {
let user = item ["fromUser"] as! User
let uid = user.uid
print("user id: \(uid)")
DataService.run.observeUser(forUserId: uid, handler: { (user) in
self.users.append(user)
})
}
self.invites = array
self.collectionView.reloadData()
}
}//end func