firebase 查询如何从子项中检索两个特定值

firebase query how can i retrieve two specific values from child

我使用 recyclerview 检索聊天消息,我想知道是否有检索两个不同值的方法,我的意思是这样的:

FirebaseRecyclerOptions<enchat> options = 
    new FirebaseRecyclerOptions.Builder<enchat>()
        .setQuery(
            mChatDatabase.orderByChild("state").equalTo("sent")
            && mChatDatabase.orderByChild("state").equalTo("received"),
         enchat.class)
        .build();

无法将两个值传递到一个条件中。 Firebase 实时数据库不支持此类 OR 条件。

另见:

  • Query based on multiple where clauses in Firebase
  • iOS - OR query in Firebase

常见的解决方法是:

  • 执行两个查询并将结果合并到您的应用程序代码中。但是在这种情况下,您将无法使用来自 FirebaseUI 的适配器。

  • 执行范围查询,但这仅在 receivedsent 之间没有 state 值时有效。如果是这样,查询可以是:

    mChatDatabase.orderByChild("state").startAt("received").endAt("sent")
    
  • 添加一个额外的 属性,当 statereceivedsent 时为真,并对其​​进行过滤:

    mChatDatabase.orderByChild("stateIsReceivedOrSent").equalTo(true)