简体   繁体   English

Android Firebase查询范围(上,中,下)

[英]Android Firebase query with range (Above, Mid, Bottom)

I just start using Firebase in Android and I wrote query like this. 我刚开始在Android中使用Firebase,并编写了这样的查询。

messageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
    .child(conversationID).limitToLast(INITIAL_MESSAGE_COUNT)
    .orderByChild("timestamp").startAt(participant.joiningDate).endAt(endAt);

I can only give limitToFirst , limitToLast , startAt , endAt . 我只能给limitToFirstlimitToLaststartAtendAt (I think there is no center) (我认为没有中心)

I need to give timestamp (eg 10 am sharp) and i wanna query 10 message before 10 am and 10 message after 10 am. 我需要提供时间戳(例如10 am),我想在10 am之前查询10条消息,在10 am之后查询10条消息。 (but i don't know when it start, when it end). (但我不知道它什么时候开始,什么时候结束)。

Edit 编辑

What I want to do is something like this. 我想做的就是这样。

messageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message").
                    child(conversationID).
                    orderByChild("timestamp").equalTo(endAt).limitToFirst(INITIAL_MESSAGE_COUNT).limitToLast(INITIAL_MESSAGE_COUNT);

But if I do, it crash like this. 但是,如果这样做,它会像这样崩溃。

java.lang.IllegalArgumentException: Can't call limitToLast on query with previously set limit! java.lang.IllegalArgumentException:无法在具有先前设置的限制的查询上调用limitToLast!

How shall I do? 我该怎么办?

My data in firebase is something like this. 我在firebase中的数据是这样的。

在此处输入图片说明

You can select a range by combining startAt and endAt , both of which operate on the child you order on. 您可以通过组合startAtendAt来选择一个范围,这两种操作都针对您订购的子级。 For example if you want messages between 10am and 10pm on a certain date, you'd do: 例如,如果您希望在某个日期的上午10点至晚上10点之间发送消息,则可以执行以下操作:

long timestampOf10am = ...;
long timestampOf10pm = ...;
messageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
  .child(conversationID)
  .orderByChild("timestamp").startAt(timestampOf10am).endAt(timestampOf10pm)
  .limitToLast(INITIAL_MESSAGE_COUNT);

Update : If you need 10 items before 10am and 10 items after 10am, you'll need to fire two queries and merge the results client-side. 更新 :如果您需要在上午10点之前添加10个项目,并在上午10点之后添加10个项目,则需要触发两个查询并在客户端合并结果。

long timestampOf10am = ...;
beforeMessageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
  .child(conversationID)
  .orderByChild("timestamp").startAt(timestampOf10am)
  .limitToFirst(INITIAL_MESSAGE_COUNT);
afterMessageSearchQuery = FirebaseDatabase.getInstance().getReference().child("Message")
  .child(conversationID)
  .orderByChild("timestamp").endAt(timestampOf10am)
  .limitToLast(INITIAL_MESSAGE_COUNT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM