[英]How can I get results for both existing and new children in Firebase?
I'm trying to create a query in Firebase for my swift iOS app. 我正在尝试在Firebase中为我的快速iOS应用程序创建查询。 The problem I'm having with the query is that it does not get the coordinate in the immediately from firebase, unless they are changed.
我在查询中遇到的问题是,除非更改它们,否则它不会立即从firebase中获取坐标。 I tried every other observer type, but none appear to work.
我尝试了其他所有观察者类型,但似乎没有一个起作用。 I know I currently have the observer type to change, but I need the correct way to make it get the location as soon as the app is loaded and also update with firebase.
我知道我当前有要更改的观察者类型,但是我需要正确的方法来使它在应用程序加载后立即获取位置,并使用firebase进行更新。
.childAdded
gets location immediately but it does not update when they are changed on Firebase. .childAdded
立即获取位置,但是在Firebase上更改它们时不会更新。
userDirectory.queryOrderedByChild("receiveJobRequest")
.queryEqualToValue(1)
.observeEventType(.ChildChanged , withBlock: {snapshot in
var cIhelperslatitude = snapshot.value["currentLatitude"]
var cIhelperslongitude = snapshot.value["currentLongitude"]
If you want to listen to multiple event types, you'll need to register multiple listeners. 如果要侦听多种事件类型,则需要注册多个侦听器。
let query = userDirectory.queryOrderedByChild("receiveJobRequest")
.queryEqualToValue(1)
query.observeEventType(.ChildAdded, withBlock: {snapshot in
var cIhelperslatitude = snapshot.value["currentLatitude"]
var cIhelperslongitude = snapshot.value["currentLongitude"]
query.observeEventType(.ChildChanged, withBlock: {snapshot in
var cIhelperslatitude = snapshot.value["currentLatitude"]
var cIhelperslongitude = snapshot.value["currentLongitude"]
You'll probably want to refactor that common code into a method, that you them call from the .ChildAdded
and .ChildChanged
blocks. 您可能需要将这些通用代码重构为一种方法,您可以从
.ChildAdded
和.ChildChanged
块中调用它们。
Alternatively you can register for a .Value
event, which is triggered for the initial value and every time the value under the query is changed. 或者,您可以注册一个
.Value
事件,该事件将为初始值以及每次查询下的值更改时触发。 But since .Value
is invoked with all matching children, you'll have to then loop over the children in your block: 但是,由于
.Value
会与所有匹配的子级一起调用,因此您必须在块中循环遍历这些子级:
query.observeEventType(.Value, withBlock: {allsnapshot in
for snapshot in allsnapshot.children {
var cIhelperslatitude = snapshot.value["currentLatitude"]
var cIhelperslongitude = snapshot.value["currentLongitude"]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.