Ios Firebase观察者最初设置值,但在进行更改时不更新
我尝试设置一个函数来侦听数据库某个部分中的更改:Ios Firebase观察者最初设置值,但在进行更改时不更新,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我尝试设置一个函数来侦听数据库某个部分中的更改: private func observeParticipants() { let databaseRef = FIRDatabase.database().reference() let groupRef = databaseRef.child("groups").child(currentRoomID).child("participants") groupRef.observe(.childAdded, with:
private func observeParticipants() {
let databaseRef = FIRDatabase.database().reference()
let groupRef = databaseRef.child("groups").child(currentRoomID).child("participants")
groupRef.observe(.childAdded, with: { snapshot in
print("observe snapshot.value: \(snapshot.value)")
if let snapDict = snapshot.value as? [String:AnyObject] {
for each in snapDict {
let uid = each.key
let avatar = each.value["profilePicture"] as! String
let gender = each.value["gender"] as! String
let handle = each.value["handle"] as! String
let name = each.value["name"] as! String
let status = each.value["status"] as! String
// Set those to the dictionaries [UID : value]
self.avatarDictionary.setValue(avatar, forKey: uid)
self.nameDictionary.setValue(name, forKey: uid)
self.genderDictionary.setValue(gender, forKey: uid)
self.handleDictionary.setValue(handle, forKey: uid)
self.statusDictionary.setValue(status, forKey: uid)
print("\n\navatarDictionary:\n \(self.avatarDictionary)")
print("\nhandleDictionary:\n \(self.handleDictionary)")
print("\ngenderDictionary:\n \(self.genderDictionary)")
print("\nnameDictionary:\n \(self.nameDictionary)")
print("\nstatusDictionary:\n \(self.statusDictionary)")
self.navBarCollectionView.reloadData()
}
}
})
}
当我运行它时,它会加载正确的值,但是如果我进行了更改,例如向组中添加新用户,它不会反映该更改。数据库更新得很好,但观察者没有看到。我有没有办法重新写这篇文章,以便正确观察?如果有帮助的话,我可以发布我的数据库结构
谢谢
编辑:数据库结构:
编辑:snapshot.value添加第二个用户后:
Snapshot.value: Optional({
gender = female;
handle = sav;
name = Savina;
profilePicture = "https://graph.facebook.com/1929991317219760/picture?type=large&return_ssl_resources=1";
status = F8B016;
})
编辑2:
这是用于最初获取所有用户值的代码,每次我离开视图并返回时也是如此。它只是不能正确地充当观察者,即如果我添加一个参与者,它就看不到这一点,并更新必要的值,例如集合视图。我必须离开这里,回来让这一切发生
func getParticipantInfo() {
let databaseRef = FIRDatabase.database().reference()
let groupRef = databaseRef.child("groups").child(currentRoomIdGlobal)
groupRef.observe(.childAdded, with: { snapshot in
if let snapDict = snapshot.value as? [String : AnyObject] {
for each in snapDict {
let uid = each.key
let avatar = each.value["profilePicture"] as! String
let gender = each.value["gender"] as! String
let handle = each.value["handle"] as! String
let name = each.value["name"] as! String
let status = each.value["status"] as! String
// Set those to the dictionaries [UID : value]
self.avatarDictionary.setValue(avatar, forKey: uid)
self.nameDictionary.setValue(name, forKey: uid)
self.genderDictionary.setValue(gender, forKey: uid)
self.handleDictionary.setValue(handle, forKey: uid)
self.statusDictionary.setValue(status, forKey: uid)
print("\n\navatarDictionary:\n \(self.avatarDictionary)")
print("\nhandleDictionary:\n \(self.handleDictionary)")
print("\ngenderDictionary:\n \(self.genderDictionary)")
print("\nnameDictionary:\n \(self.nameDictionary)")
print("\nstatusDictionary:\n \(self.statusDictionary)")
self.navBarCollectionView.reloadData()
}
}
})
}
你能发布你的数据库结构吗?给你:如果currentRoomId是你刚才在“groups”下面的childByAutoID,我认为你应该观察“参与者”。试试看!我的意思是让groupRef=databaseRef.child(“groups”).child(currentRoomID).child(“参与者”)在
.child(currentRoomID)
之后添加.child(“参与者”)
会导致崩溃,在循环的第一个值上意外地发现零(让avatar=each.value[“profilePicture”]as!String
)