Firebase QueryOrderedByChild以及EqualToValue和StartingAtValue组合 [英] Firebase QueryOrderedByChild along with EqualToValue and StartingAtValue Combination

查看:413
本文介绍了Firebase QueryOrderedByChild以及EqualToValue和StartingAtValue组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个等于某个特定值的子查询Firebase,但是在特定的发布键上启动快照。

数据结构:

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

示例:

我试图用<$ c $ queryOrderedByChild(storeId) c> queryEqualToValue(2)但是我希望快照在按照顺序返回的时候从postKey4开始。

我当前的查询:
$ b

  ref.queryOrderedByChild(storeId)。q​​ueryEqualToValue(\我知道你可以通过这个方法来创建一个新的表单。在 queryStartingAtValue  queryEndingAtValue 或<$之后调用 queryEqualToValue: c $ c> queryEqualToValue 之前被调用,但我需要 queryEqualToValue 来获得我的storeId。您可以将第二个参数传递给 queryStartingAtValue   c $> b 
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ 2,childKey:postKey4)
.queryEndingAtValue(2)
.observeEventType(.Value,withBlock:{快照在
中用于snapshot.children中的childSnapshot {
print(childSnapshot .key !!
}
})

打印:


postKey4



postKey6


我必须承认这是我第一次使用第二个参数到 queryStartingAtValue ,所以我可能还不完全理解它。



你不能使用 queryEqualToValue 的原因是它只会返回一个单一的键,这不是你想要的。

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!

解决方案

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.

这篇关于Firebase QueryOrderedByChild以及EqualToValue和StartingAtValue组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆