您尚未定义平台,因此我将使用此Swift伪代码示例。假设我们想一次获得所有用户的列表。
let usersRef = my_firebase.child("users")
usersRef.observeSingleEvent(by: .value,with: { snapshot in
//all users are returned to the app in the snapshot
// .value means 'give me everything in the specified node'
// so then we can iterate over the snapshot to get each users data
}
您可以随时调用上述代码来获取用户列表,并且不会在节点上添加观察者(侦听器)。一口气。
如果希望收到添加
用户的通知,
let usersRef = my_firebase.child("users")
usersRef.observe(by: .childAdded,with: { snapshot in
//upon first calling this,each user will be sent to the app in the snapshot
// and after that,any user that's added
}
以上代码将观察者(侦听器)附加到用户节点,并且每当添加用户时,便会通过快照将其提供给应用。
请注意,使用.observeSingleEvent与.observe和.value来获取节点中的所有内容,而使用.childAdded来获取特定节点。
本文链接:https://www.f2er.com/3168522.html