【问题标题】:firebase query how can i retrieve two specific values from childfirebase 查询如何从子项中检索两个特定值
【发布时间】:2021-01-09 01:15:18
【问题描述】:

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

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

【问题讨论】:

    标签: android firebase-realtime-database android-recyclerview firebaseui


    【解决方案1】:

    无法将两个值传递给条件。 Firebase 实时数据库不支持这种类型的 OR 条件。

    另见:

    常见的解决方法是:

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

    • 执行范围查询,但这仅在receivedsent 之间没有state 值时才有效。如果是这种情况,查询可能是:

      mChatDatabase.orderByChild("state").startAt("received").endAt("sent")
      
    • statereceivedsent 时添加一个额外的属性并过滤:

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

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2017-09-16
      • 2018-07-26
      • 2018-08-11
      相关资源
      最近更新 更多