问题
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