我正在尝试获取我的消息节点的最后一个id,但它没有得到最后一个。例如,我的最后一个id是12,但它总是返回第10个项目的id。我的代码如下:
final Query query = firebase.orderByChild("messageId").limitToLast(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i("MessagesFrag","snapshot: "+dataSnapshot.getValue()+"");
}
public void onCancelled(FirebaseError firebaseError) {}
});
它返回类似于这样的内容:
{10={chatId=6, time=Wed Apr 13 22:20:43 EDT 2016, message=hi, user=123, messageId=10}}
它应该返回:
{12={chatId=6, time=Wed Apr 13 22:29:02 EDT 2016, message=I am fine, user=1234, messageId=12}}
我的数据看起来像这样:
messages
9:
chatId: "6"
message: "hi"
messageId: "9"
time: "Wed Apr 13 22:29:02 EDT 2016"
user: "123"
10:
chatId: "6"
message: "hello"
messageId: "10"
time: "Wed Apr 13 22:29:02 EDT 2016"
user: "1234"
11:
chatId: "6"
message: "how are you?"
messageId: "11"
time: "Wed Apr 13 22:29:02 EDT 2016"
user: "123"
12:
chatId: "6"
message: "I am fine"
messageId: "12"
time: "Wed Apr 13 22:29:02 EDT 2016"
user: "1234"
除此之外,当我将 "LimitToLast" 更改为 "LimitToFirst"(应该获取第一个元素)时,它并没有获取到我拥有的第一个元素(节点1),它还获取了第10个节点。有人知道可能是什么问题吗?