gpt4 book ai didi

java - Firebase 在不同设备/Android 版本上以不同顺序返回子节点的 key

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:08 25 4
gpt4 key购买 nike

我正在从我的 Firebase 数据库获取数据快照以检索用户列表,但返回的 key 顺序因所使用的 Android 版本/设备而异。

出于演示目的,我缩短了该方法,但它基本上如下所示:

public void getUsers(){
Firebase ref = new Firebase("https://myFirebaseID.firebaseio.com");
final Firebase userRef = ref.child("users");

userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
snapshot.toString();
}
});
}

我通过调用快照对象 (snapshot.toString()) 上的 toString() 获得的数据改变了顺序。

我已经在 4 台设备上试过了。 2 个正在运行的 Lollipop(Nexus 7 5.1.1 和 Galaxy s4 5.01)以相同的顺序返回数据。而另外两个设备(HTC Sensation 4.0.3 和 Motorola G2 4.4.4)以相同的顺序返回数据(但与 Lollipop 设备的顺序不同)。

使用的代码没有区别,在我检索快照时数据库中的数据完全没有变化。

这是 4.4.4 和 4.0.3 设备上的数据顺序:

DataSnapshot { 
key = users,
value = {
114585619420240714499={
**userIDOfCUser**=114585619420240714499,
**NameOfCUser**=testName,
**EmailOfCUser**=testerfireapp@gmail.com,
**friends**={
103902248954972338254={
**userIDOfFriend**=103902248954972338254,
**NameOfFriend**=testName2
}
}
}

这是 5.1.1 和 5.01 设备上的数据顺序:

DataSnapshot { 
key = users,
value = {
114585619420240714499={
**NameOfCUser**=testName,
**userIDOfCUser**=114585619420240714499,
**friends**={
103902248954972338254={
**NameOfFriend**=testName2 ,
**userIDOfFriend**=103902248954972338254
}
},
**EmailOfCUser**= testerfireapp@gmail.com
}
}
}

为什么根据所使用的 Android 版本/设备以不同的顺序传送数据?还有其他我不知道的区别吗?

编辑:当按如下方式遍历快照时,键的不同顺序在不同版本的 Android 中仍然存在:

public void getUsers2(){

Firebase ref = new Firebase("https://myFirebaseID.firebaseio.com");
final Firebase userRef = ref.child("users");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot keys : snapshot.getChildren()) {
//String to temporarily store the whole child of the inidividual user's DB node ----> It still produces different order of the keys
String tempKey = keys.getValue().toString();

//The problem persists if I code it like this as well.
String tempKey2 = snapshot.child(keys.getKey()).getValue().toString();

}
}
});
}

最佳答案

Firebase's REST API contains this warning 的文档:

When using the REST API, the filtered results are returned in an undefined order since JSON interpreters don't enforce any ordering. If the order of your data is important you should sort the results in your application after they are returned from Firebase.

换句话说:JSON 对象中的属性可以按任何顺序排列。 JSON 解析器可以根据需要自由地重新排列它们。

您使用的不是 REST API,而是 Firebase Android SDK。如果向您隐藏此类重新排序或(当无法隐藏时)明确告诉您订单是什么(例如 previousChildName argument to the onChildAdded callback)

但是从您显示的数据来看,您似乎正在解析 dataSnapshot.toString() 的输出。虽然这是完全正确的,但这确实意味着您选择自己处理订单。一般来说,最好坚持使用 Firebase Android SDK 的方法,因为它们会处理诸如为您订购之类的事情:

public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot friendSnapshot: snapshot.getChildren()) {
System.out.println(friendSnapshot.child("NameOfFriend").getValue());
}
}

更新

从您的更新来看,您似乎对用户有疑问,然后还想遍历他们的 friend 。使用 Firebase Android API,您可以使用:

Firebase ref = new Firebase("https://myFirebaseID.firebaseio.com");
final Firebase userRef = ref.child("users");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot userSnapshot) {
DataSnapshot friendsSnapshot = userSnapshot.child("friends");
for (DataSnapshot friendSnapshot : friendsSnapshot.getChildren()) {
System.out.println(friendSnapshot.child("NameOfFriend").getValue());
}
}
});

关于java - Firebase 在不同设备/Android 版本上以不同顺序返回子节点的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31808990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com