I am getting a snapshot of the data from my Firebase database to retrieve a users list, but the order of the keys being returned is different depending on the Android version/ device being used.
For demonstrative purposes I have shortened the method, but it is essentially as follows:
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();
}
});
}
It is the data I get from calling toString() on the snapshot object (snapshot.toString()) that changes order.
I have tried it on 4 devices. The 2 running Lollipop (Nexus 7 5.1.1 & Galaxy s4 5.01) return the data in the same order. And the 2 two other devices (HTC Sensation 4.0.3 and Motorola G2 4.4.4) return the data in the same order (but a different order to devices with Lollipop).
There is no difference in the code used, and the data in the database was completely unchanged at the times when I retrieved the snapshots.
Here is the data order on the 4.4.4 and 4.0.3 devices:
DataSnapshot {
key = users,
value = {
114585619420240714499={
**userIDOfCUser**=114585619420240714499,
**NameOfCUser**=testName,
**EmailOfCUser**[email protected],
**friends**={
103902248954972338254={
**userIDOfFriend**=103902248954972338254,
**NameOfFriend**=testName2
}
}
}
Here is the data order on the 5.1.1 and 5.01 devices:
DataSnapshot {
key = users,
value = {
114585619420240714499={
**NameOfCUser**=testName,
**userIDOfCUser**=114585619420240714499,
**friends**={
103902248954972338254={
**NameOfFriend**=testName2 ,
**userIDOfFriend**=103902248954972338254
}
},
**EmailOfCUser**= [email protected]
}
}
}
Why is the data being delivered in different orders depending on the android version/device being used? is there another difference I am unaware of?
Edit:
When iterating through the snapshot as follows, the different ordering of the keys still persists accross different versions of 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();
}
}
});
}
See Question&Answers more detail:
os