Firebase Database ArrayList is not ordered - android

Why is the list I retrieve from the Firebase Database not ordered with orderByKey()?
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
transactions = mDatabase.child("balance").child("users").child(userid).child("log").orderByKey();
transactions.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Map<String, Object> td = (HashMap<String,Object>) snapshot.getValue();
ArrayList<Object> values = new ArrayList<Object>();
values.addAll(td.values());
Log.d("Transactions", values.toString());
}
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
});
The ArrayList "values" is unordered.
This is an example of the structure in json format:
"balance" : {
"users" : {
"log" : {
"-Kp0plO6zBeulHQ8S1Fj" : {
"In" : 100
},
"-Kp0qHM73Mcwbli1gaK6" : {
"Out" : -100
},
"-Kp0qgkORJx5sTi2rUZf" : {
"In" : 100
},
"-Kp1OCdSpUA7SEZhVoPR" : {
"In" : 100
},
"-Kp1OdtuuF6z_zlNj2St" : {
"In" : 100
}
}
}
}

When you retrieve a collection from Firebase, it receives three pieces of information for each child node:
the key of the child
the value of the child
the relative order of the child
When you call snapshot.getValue() in your current code, you tell Firebase to convert this into a Map<String, Object>. Since a Map is unordered, the Firebase client keeps the keys and values but loses the ordering of the children.
To ensure you keep the order, you must use DataSnapshot.getChildren() to process the result:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
transactions = mDatabase.child("balance").child("users").child(userid).child("log").orderByKey();
transactions.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
ArrayList<Object> values = new ArrayList<Object>();
for (DataSnapshot child: snapshot.getChildren)) {
values.addAll(child.getValue());
}
}

To achieve what you want you need to change this line of code:
transactions = mDatabase.child("balance").child("users").child(userid).child("log").orderByKey();
with
transactions = mDatabase.child("balance").child("users").child(userid).child("log").orderByChild("In");

Related

Firebase Listener Skip

I have converted to Firebase for my Android development as I want to utilise the many features of Firebase (Notifications, Messaging, Storage etc), however, I am having difficulty receiving data from Firebase and cannot find detailed information online. Yes, I have tried the documentation but it does not explain the events I am experiencing.
I want to retrieve all the data that I have an insert it into an arraylist of objects. Here is a static example:
users[i] = new User("James" + (i*2.5), "Hmack00" + (i*2), "https://firebasPic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=cf", "United Kingdom");
Database:
{
"Users" : {
"4PdlTlv3qjZ3BmDvrJyUut9Fnq43" : {
"Country" : "xx",
"Fullname" : "hh",
"ProfilePicture" : "htm/o/Images%2FSearchAdapter%2F4PdlTlv3qjZ3BmDvrJyUut9Fnq43%2FProfilePicture%2FProfilePic_2019.03.06.10.47.54.jpg.jpg?alt=media&token=b647708e-c6d5-4b45-bef0-3dc40301b73a",
"Username" : "hmack001"
},
"COg4r4io9hezhFpmK3adPucUXA93" : {
"Country" : "spain",
"Fullname" : "nat",
"ProfilePicture" : "hcom/o/Images%2FSearchAdapter%2FCOg4r4io9hezhFpmK3adPucUXA93%2FProfilePicture%2FProfilePic_2019.03.06.19.14.17.jpg.jpg?alt=media&token=8620b321-5cef-42f0-a828-dbb7c37c8e7d",
"Username" : "nat"
},
"Tw1xRxViygNsLqrQiaaMAvAduIu1" : {
"Country" : "uk",
"Fullname" : "harvey\n",
"ProfilePicture" : "t.com/o/Images%2FUsers%2FTw1xRxViygNsLqrQiaaMAvAduIu1%2FProfilePicture%2FProfilePic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=c290e75a-5f92-4271-bcb5-c644fe1b14ef",
"Username" : "RGB"
},
"vOxr1RoDqgWogKK1lp9pfpTHc6w2" : {
"Country" : "scotland ",
"Fullname" : "greg greg",
"ProfilePicture" : "ot.com/o/Images%2FSearchAdapter%2FvOxr1RoDqgWogKK1lp9pfpTHc6w2%2FProfilePicture%2FProfilePic_2019.03.04.12.30.22.jpg.jpg?alt=media&token=27b024cf-0691-4121-8a27-26acf101ebc2",
"Username" : "greg"
},
"xecUOPeyMcQaQrgkU9ouDgK90Ai1" : {
"Country" : "ggh",
"Fullname" : "Da apply ",
"ProfilePicture" : "2FProfilePic_2019.03.03.04.58.50.jpg.jpg?alt=media&token=f35854c2-3ff9-4d18-9f7a-10c13f066c68",
"Username" : "gg"
}
}
}
Here is my code and I will explain my errors after (I have left in 'zombie' code, to show the attempts that I have made)
//Firebase Variables
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
//Firebase Data
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
userId = mAuth.getCurrentUser().getUid();
myRef = mFirebaseDatabase.getReference().child("Users").child(userId);
//Firebase Data
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) { //Function called every time a change is made to the database
showData(dataSnapshot);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) { //Function called when there is an error in the database/ with the call
Log.d("Print","Cancelled Firebase: " + databaseError.toString());
}
});
}
private void showData(DataSnapshot dataSnapshot) {
int userCount = 5;
int i = 0;
for (DataSnapshot ds : dataSnapshot.getChildren()) {
//User user = new User();
//User user = new User(); //Get Count of Firebase Users
//user = new User();
if (ds.child(userId).exists()) {
/*user.setFullname(ds.child(userId).getValue(User.class).getFullname()); //set the full name
user.setUsername(ds.child(userId).getValue(User.class).getUsername()); //set the username
user.setProfilePicture(ds.child(userId).getValue(User.class).getProfilePicture()); //set the profile picture
//Display Information
Log.d("DataSnapchat Test", "ShowData Name: " + user.getFullname());
Log.d("DataSnapchat Test", "ShowData Username: " + user.getUsername());
Log.d("DataSnapchat Test", "ShowData Picture: " + user.getProfilePicture());
ArrayList<String> userArrayList = new ArrayList<>();
userArrayList.add(user.getFullname());
userArrayList.add(user.getUsername());
userArrayList.add(user.getProfilePicture());
*/
String fullname = (String) ds.child(userId).child("Fullname").getValue();
Toast.makeText(this, "Fullname: " + fullname, Toast.LENGTH_SHORT).show();
//UserListAdapter adapter = new UserListAdapter(this, R.layout.find_profiles_search, userArrayList, mProfileSearch);
//mListView.setAdapter(adapter);
i++;
}
}
}
When I debug the code the showData function is never called and neither functions within the Value Event Listener are called, is this an Async Problem?
Currently I am trying to fetch data and insert it into a variable (Literally any data, once I have a working query I can convert it to fit by manipulating child fields etc).
Question: Does the addValueListener only work when data is changed in the database? If so then what is the alternative, if not then why are the functions not operating.
I do not recieve any errors and nothing is logged to the console.
I want to put my database into an array list, I know that I shouldnt use getChildren to do this, put I am trying to test if I can get any data before I try and get all the data.
You have some really weird loops in showData(). Since you're attaching a ValueEventListener to the node of the specific user, you can just look up the property values for that specific user:
myRef = mFirebaseDatabase.getReference().child("Users").child(userId);
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("OnDataChange", dataSnapshot.getKey());
Log.d("OnDataChange", dataSnapshot.child("Fullname").getValue(String.class));
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("Print","Cancelled Firebase: " + databaseError.toString());
}
});

Error with query startAt and endAt in Firebase

This is my structure:
{
users: {
"aufgdheyrl": {
id: "152",
name: "Joe Jackson",
level: "A2",
point: 120.0
},
"yudwjwnecj": {
id: "134",
name: "Samnuel Jackson",
level: "B3",
point: 80.5
},
"fuwhdkcjdo": {
id: "188",
name: "Jack Jack",
level: "B2",
point: 50.0
}
}
}
this is my code for get the selected user only if its level or point value is greater than another one. But the query doesnt't work.
I use mDatabase.child("users").child(id_client) for getting the reference of the selected user.
DatabaseReference mUserRef = mDatabase.child("users").child(id_client);
Query query1 = mUserRef.orderByChild("level").startAt(level);
Query query2 = mUserRef.orderByChild("point").startAt(point);
query1.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "datasnaphot " + dataSnapshot.toString());
User user = dataSnapshot.getValue(User.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
query2.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "datasnaphot " + dataSnapshot.toString());
User user = dataSnapshot.getValue(User.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
for example I have this values:
String id_client = "aufgdheyrl";
double level = 200.0;
String point = "A1";
both query1 and query2 return object but query1 must have null value and not user object
Idem if I have level="B1", the query2 return user object and not null value
Firebase queries are executed on a list of nodes, and then filter each child node in that list on the property that you specify.
With mDatabase.child("users").child(id_client) you're already in the node of a specific user:
"aufgdheyrl": {
id: "152",
name: "Joe Jackson",
level: "A2",
point: 120.0
}
Calling orderByChild() here will try to filter each child node of this JSON. Since the child nodes are simple values (e.g. point: 120.0), the filter will not match anything.
You're more likely to get the results you want if you query the list of users for a level:
Query query = mDatabase.child("users").orderByChild("level").startAt("B3");
This query will return a list with yudwjwnecj.

Firebase Console Database Rules : how to index the following database structure in Database Rules

The example is parking spaces listing in a country.
The reason to structure the database with a userParkingDistanceMap is sorting by distance between the current user and all parking spaces.
The database query is .orderByChild("userParkingDistanceMap/" + getUid()).
So, how could I index this userParkingDistanceMapin Database Rules?
"parkingSpacesTokyo" : {
"parkingId5" : {
"parkingName" : "MY-PARKING-NAME",
"parkingQuantity" : 70,
"userParkingDistanceMap" : {
"uid-uid-uid-uid-uid-uid-uid" : 7983,
"9EAiImOh4ZUnqJ7em51u81FukD73" : 9456,
"AG84p8KJgANuobRJ5pn1ipxULFB3" : 8336,
"yYLVffMFsBQhsGHNWuPOksMDgip2" : 7983,
"yp6mZm2SPVhz5pOq57tSEZ40piJ2" : 7983
}
},
"parkingId6": {
...
}
},
"parkingSpacesYOKOHAMA" : {
}
So basically you might have initialized the reference by something like
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("parking");
ref.addListenerForSingleValueEvent(valueEventListner);
where valueEventListner is:
ValueEventListner valueEventListner = new ValueEventListener()
{
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for(DataSnapshot childDataSnapshot: dataSnapshot)
{
DatabaseReference parkindId = ref.child(dataSnapshot.getKey);
for(DataSnapshot parkingIdSnapshot : childDataSnapshot)
{
if(parkindIdSnapshot.hasChildren())
{
for(DataSnapshot distanceMap : parkingIdSnapshot)
{
String value = distanceMap.getValue(String.class);
}
}
}
}
}
#Override
public void onCancelled(DatabaseError databaseError)
{}
});
Well I just wrote this code in direct answer so it's kind of psudo code so basically you might have got the idea about it. Best of luck.

How to structure my sport manager Firebase database

I'm building a sport manager app that let the users create their own competitions and update the match results to be consults for other participants.
I read a lot about how flat the database (I come from SQL :-) and saw many video tutorials from the Firebase Team.
At this point I have this structure:
{sport-manager :
...
groups : {
"groupId1" : {
competitionId : "competitionId1"
name : "Group A",
type : "LEAGUE"
},
"groupId2" : {
competitionId : "competitionId1"
name : "West playoofs",
type : "PLAYOFFS"
},
...
},
players : {
"playerId1" : {
birthday : 351408600000,
firsName : "Jhon",
lastName : "Doe",
shirtNumber : 0,
teamId : "teamId1"
},
...
},
teams : {
"teamId1" : {
competitionId : "competitionId1"
name : "Team 1",
},
...
},
teamGroupStats : {
"groupId1" : {
"teamId1" : {
goalsAgainst: 0,
goalsFavor: 0,
matchesDraw: 0,
matchesLoss: 0,
matchesPlayed: 0,
matchesWin: 0,
points: 0,
teamName: "Team 1",
},
"teamId2" : {
...
},
...
},
"groupId2" : {
"teamId2" : {
...
},
...
}
...
}
}
This structure let me make queries to populate a ListView adapter with the data of all the teams that are part of a group in this way:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
Query groupQuery = mDatabase.child("teamGroupStats").child("groupId1")
Or when I have to get the player from determinated team:
Query playersQuery = mDatabase.child("players").orderByChild("teamId").equalTo("teamId1");
So far, so good. My problem come when I have to delete a team from this structure. For avoid integrity problems I use multipath update to delete all.
A team can have one or more players
A team can be part of one or more groups.
With that in mind, I can delete any teams with all his players.
Map<String, Object> deleteMap = new HashMap<>();
deleteMap.put("teams/teamId1", null);
Query playersQuery = mDatabase.child("players").orderByChild("teamId").equalTo("teamId1");
playersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot playerSnapshot : dataSnapshot.getChildren()){
deleteMap.put("players/" + playerSnapshot.getKey(), null);
}
// Delete all in one call!
mDatabase.updateChildren(deleteMap);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
How I can make a query to get all the paths with "teamId1" has secundary key in the structure "teamGroupStats"?
His this structure good for the use I need?
IMHO your database is very well structured. Regarding your question, you cannot get all paths as you say. What can you do, is to delete data in the same way as you add it. So in order to delete all data i recomand you using this method:
private static void deleteTeam(String teamId) {
Map<String, Object> map = new HashMap<>();
map.put("/teams/" + teamId, null);
map.put("/teamGroupStats/groupId1/" + teamId, null);
map.put("/players/playerId1/teamId/", null);
databaseReference.updateChildren(map);
}
A more generic method will be:
private static void deleteTeam(String reference, String teamId) {
Map<String, Object> map = new HashMap<>();
map.put(reference, null);
databaseReference.updateChildren(map);
}
If you need to query the database to get additional data, use getRef() method to get the reference and pass those values to the second method.
All your data will be deleted at once atomically.
Hope it helps.
Finally I found one solution. I modified my database to store all the groups Id's where the team belong inside the Team structure.
teams : {
"teamId1" : {
competitionId : "competitionId1"
groups : {
"groupId1" : true,
"groupId6" : true,
...
},
name : "Team 1",
},
...
With that information, I can add an inner ValueEventListener in the playersQuery's listener and make the update/delete.
public static void deleteTeam(final String teamId){
Map<String, Object> deleteMap = new HashMap<>();
deleteMap.put("teams/" + teamId, null);
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
Query playersQuery = mDatabase.child("players").orderByChild("teamId").equalTo(teamId);
playersQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot playerSnapshot : dataSnapshot.getChildren()){
String playerId = playerSnapshot.getKey();
deleteMap.put("players/" + playerId, null);
}
mDatabase.child("teams").child(teamId).child("groups")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()){
String groupId = groupSnapshot.getKey();
deleteMap.put("teamGroupStats/" + groupId + "/" + teamId, null);
}
// Delete all in one call!
mDatabase.updateChildren(deleteMap);
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
I wasn't very sure about the inner listener, and I had to made a little change to my database structure, but this works fine.
If anybody knows a better way to do this, I'm open to read it. Thanks!

Get children with unknown keys in Firebase

I'm making a blog posting app. I need to display the posts with the name of the author and the time.
I'm adding the post to database at posts/uid/key This key is added by Firebase on pushing the values. I cannot set this key to a fixed value as each post has different name, time, and body.
Now while retrieving the data, I want to know how to get the data at posts/uid/key/
I'm using this loop to traverse all the children of posts/ But I cannot go further into the nesting as I don't know what the keys are
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String uid = (String) postSnapshot.child("uid").getValue();
String body = (String) postSnapshot.child("body").getValue();
}
}
I'm attaching the above listener to this reference
mPostReference = FirebaseDatabase.getInstance().getReference().child("posts");
How do I get the children of the keys without getting what the keys are?
private DatabaseReference mPostReference;
mPostReference = FirebaseDatabase.getInstance().getReference()
.child("posts");
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String uid = (String) postSnapshot.child("uid").getValue();
String body = (String) postSnapshot.child("body").getValue();
Toast.makeText(getActivity(), body,
Toast.LENGTH_SHORT).show();
}
}
Exported JSON
{
"posts" : {
"-KTGou5X14D8pq1sxyhR" : {
"body" : "first post",
"timestamp" : 1475620339666,
"uid" : "tmFt3NbcnLVlkrWQrIx1"
},
"-KTGp3mfM4krbGoTMZ8t" : {
"body" : "second post",
"likeCount" : 0,
"uid" : "tmFt3NbiwLVlkrWQrIx1"
}
}
}

Categories

Resources