Firebase QueryOrderedByChild along with EqualToValue and StartingAtValue Combination

允我心安 提交于 2020-01-05 05:32:07

问题


I am trying to query Firebase by a Child that is equal to a certain value BUT start the snapshot at a specific post key.

Data Structure:

projectName
  -posts
    -postKey1
      -storeId: 1
    -postKey2
       -storeId: 2
    -postKey3
       -storeId:3
    -postKey4
       -storeId:2
    -postKey5
       -storeId:3
    -postKey6
       -storeId:2

Example:

I am trying to queryOrderedByChild("storeId") with queryEqualToValue("2") BUT I would like the snapshot to start at "postKey4" when it is returned in its order.

My Current Query:

ref.queryOrderedByChild("storeId").queryEqualToValue("\(myId)").observeEventType(.Value, withBlock: {snapshot in 

I know that you can't call queryEqualToValue: after queryStartingAtValue, queryEndingAtValue or queryEqualToValue after previously called but I need that queryEqualToValue to get my storeId. Any help is greatly appreciated!


回答1:


You can pass a second argument into queryStartingAtValue that specifies the key at which to start.

ref.queryOrderedByChild("storeId")
   .queryStartingAtValue(2, childKey: "postKey4")
   .queryEndingAtValue(2)
   .observeEventType(.Value, withBlock: {snapshot in
       for childSnapshot in snapshot.children {
           print(childSnapshot.key!!
       }
})

This prints:

postKey4

postKey6

I must admit this is the first time I use the second argument to queryStartingAtValue, so I might not understand it completely yet.

The reason you can't use queryEqualToValue is that that will only return a single key, which is not what you want.



来源:https://stackoverflow.com/questions/38933148/firebase-queryorderedbychild-along-with-equaltovalue-and-startingatvalue-combina

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!