Firebase .getvalue与数据库的顺序不同 [英] Firebase .getvalue not in the same order as database
本文介绍了Firebase .getvalue与数据库的顺序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b
当下面的代码运行时:
字符串loc (snapshot.child( LOC)的GetValue())。=的ToString();
我得到的输出与数据库的顺序不同:
为什么所以?
解决方案
Firebase数据以JSON格式存储,并且本身无序。
如果要按特定顺序访问数据,则应为查询指定 orderBy
子句,并使用快照的 getChildren( )方法。
假设您想按升序记录项目:
DatabaseReference rootRef = FirebaseDatabase.getInstance()。getRef();
查询位置= rootRef.orderByKey(); (DataSnapshot locSnapshot:snapshot.getChildren()){
System.out.println();
location.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot snapshot){
(localsnapshot.getKey()+:+ locSnapshot.getValue(String.class));
}
}
public void onCancelled(DatabaseError databaseError){
Log.w(TAG,loadPost:onCancelled,databaseError.toException());
// ...
}
});
本示例来自阅读关于数据列表的Firebase文档。
My Firebase Database is like this
When the coding below was run:
String loc=(snapshot.child("loc").getvalue()).tostring();
The output I get has different sequence with the database:
Why is that so?
解决方案
Firebase data is stored as JSON and is inherently unordered.
If you want to access the data in a specific order, you should specify an orderBy
clause for your query and access the data using the snapshot's getChildren()
method.
Say you want to log the items by ascending key:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getRef();
Query locations = rootRef.orderByKey();
locations.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot locSnapshot: snapshot.getChildren()) {
System.out.println(locSnapshot.getKey() + ": " + locSnapshot.getValue(String.class));
}
}
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
});
This sample comes (modified) from the Firebase documentation on reading lists of data.
这篇关于Firebase .getvalue与数据库的顺序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文