簡體   English   中英

如何在Firebase中獲得現有子代和新子代的結果?

[英]How can I get results for both existing and new children in Firebase?

我正在嘗試在Firebase中為我的快速iOS應用程序創建查詢。 我在查詢中遇到的問題是,除非更改它們,否則它不會立即從firebase中獲取坐標。 我嘗試了其他所有觀察者類型,但似乎沒有一個起作用。 我知道我當前有要更改的觀察者類型,但是我需要正確的方法來使它在應用程序加載后立即獲取位置,並使用firebase進行更新。 .childAdded立即獲取位置,但是在Firebase上更改它們時不會更新。

userDirectory.queryOrderedByChild("receiveJobRequest")
             .queryEqualToValue(1)
             .observeEventType(.ChildChanged  , withBlock: {snapshot in
        var cIhelperslatitude = snapshot.value["currentLatitude"]
        var cIhelperslongitude = snapshot.value["currentLongitude"]

如果要偵聽多種事件類型,則需要注冊多個偵聽器。

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"]

您可能需要將這些通用代碼重構為一種方法,您可以從.ChildAdded.ChildChanged塊中調用它們。

或者,您可以注冊一個.Value事件,該事件將為初始值以及每次查詢下的值更改時觸發。 但是,由於.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM