我的数据库结构是这样的;
"blockByUser" : {
"pcAkkvLY1TYseqk04sISUqop3Wt1" : {
"7OYw2AtvZ7hxprFiX2e1LOn1DWs2" : true,
"W4uaW1pOtkYUMh1jTJJVXQu0u6f1" : true
}
}
"users" : {
"Gn0SwQOmUvNkAFeg0YpGax75cpz2" : {
"blocked" : {
"pcAkkvLY1TYseqk04sISUqop3Wt1" : true
},
"capacity" : "NO",
"country" : "Netherlands",
"email" : "katnl"
},
"iDlNUNNAsiN0JmO8bEdYAdANcn62" : {
"blocked" : {
"Gn0SwQOmUvNkAFeg0YpGax75cpz2" : true
},
"capacity" : "NO",
"country" : "Netherlands",
"email" : "dal"
},
"pcAkkvLY1TYseqk04sISUqop3Wt1" : {
"capacity" : "NO",
"country" : "Netherlands",
"email" : "test",
}
}
我现在必须在客户端进行过滤,从blockedByUser中删除密钥,因此我有2个词典,其中一个完整的'用户列表和一个blockedByUsers,现在我遇到了更新我的collectionView的问题,因为firebase数据是异步的,我不知道如何让它像这样工作:
func fetchBlockedUsers() {
print("Fetching blocked users..")
let ref = FIRDatabase.database().reference().child("blockedByUser").child(currentUserUID)
ref.observe(.value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { return }
self.users.removeAll()
let blocked = dictionaries
let ref = FIRDatabase.database().reference().child("users")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { return }
var dict = dictionaries
for block in blocked {
dict.removeValue(forKey: block.key)
}
dict.forEach({ (key, value) in
guard let userDictionary = value as? [String: Any] else { return }
let user = UserModel(uid: key, dictionary: userDictionary)
self.users.append(user)
})
self.collectionView?.reloadData()
}) { (err) in
print("Failed to fetch users for search:", err)
}
}) { (err) in
print("Failed to fetch users for search:", err)
}
}
但也许最好将数据扇出并使用firebase功能来管理数据?