Change values in Firebase database nodes by a given criteria - android

I implemented some kind of chat app using Firebase database. Everything is set and working. Now I am trying to implement functionality that after user name changes all his messages with new username. The Message structure looks like this:
I had success whit this code:
private void changeCurrentUserMessagesUserName(final String newUserName) {
mDatabase.child(RequestParameters.FB_CHILD_MESSAGES) //messages
.orderByChild(RequestParameters.FB_CHILD_USERS_UID) //messageUserUid
.equalTo(mUser.getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
ChatMessage message = messageSnapshot.getValue(ChatMessage.class);
mDatabase.child(RequestParameters.FB_CHILD_MESSAGES) //messages
.child(message.getMessageNodeKey())
.child(RequestParameters.FB_CHILD_MESSAGES_USERS) //messageUser
.setValue(newUserName);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//do nothing
}
});
}
But it smells, if there are 100 messages in Firebase that belongs to current user this code will make 100 request to Firebase to change "messageUser" node to every single message.
Anyone could help how to make just one request that?

The link Andre provided has the gist of it: by using a single multi-location update, you can send the entire update in one call. Here's the same approach in Java:
mDatabase
.child(RequestParameters.FB_CHILD_MESSAGES) //messages
.orderByChild(RequestParameters.FB_CHILD_USERS_UID)
.equalTo(mUser.getUid())
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String,Object> updates = new HashMap<String,Object>();
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
ChatMessage message = messageSnapshot.getValue(ChatMessage.class);
updates.put(
RequestParameters.FB_CHILD_MESSAGES+"/"+
message.getMessageNodeKey()+"/"+
RequestParameters.FB_CHILD_MESSAGES_USERS,
newUserName
);
}
mDatabase.updateChildren(updates);
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}

Related

Firebase database Object.toString()' on a null object reference error

So i have a viewholder which has the name of the user and Under it i wanna put the last message that this user received , i have a query that gives me the last node in the database there is my code :
Query lastQuery = allDb.child("ReceivedMessages").child(mCurrent_user_id).child(list_user_id).orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String message = dataSnapshot.child("message").getValue().toString();
friendViewHolder.lastMessage.setText(message);
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});
and this is my database structure (also i'm sure that the referance is correct because whenever i assign all of the data to a textView it gives me the last node plus the message and date etc .. but i only want the message )
and this is my error
I hope this will do the job.
Query lastQuery = allDb.child("ReceivedMessages").child(mCurrent_user_id).child(list_user_id).orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnaphot.getChildren()) {
String message = ds.child("message").getValue().toString();
friendViewHolder.lastMessage.setText(message);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});

Display data from multiple nodes in Firebase Android

Hi I am trying to display data from multiple nodes in my database on a listview.
What I'm trying to do is display the "detailCategory" and "detailValue" for every type of workout, every time its saved.
Here is the database reference i'm currently using for my app.
currentUserDB = FirebaseDatabase.getInstance().getReference("WorkoutDetails").child("Cardio").child(currentUId);
I know this needs to increment into the further nodes, but i'm not sure how to read from multiple nodes from at one time.
I'm using dataSnapshot to read the data. I have data being read by specific date in another page, using the code below. So the problem might just be with the database reference in this case.
#Override
protected void onStart() {
super.onStart();
currentUserDB.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
cardiosList.clear();
for(DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){
Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class);
cardiosList.add(cardio);
}
CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
ListViewCardioHistory.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
As I saw in your comment, that you are "going as far as the date node", according to this, to display the detailCategory and the detailValue, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dateRef = rootRef.child("WorkoutDetails").child("Cardio").child(currentUId).child(date);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String detailCategory = ds.child("detailCategory").getValue(String.class);
String detailValue = ds.child("detailValue").getValue(String.class);
Log.d("TAG", detailCategory + " / " + detailValue);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
dateRef.addListenerForSingleValueEvent(eventListener);
Where the date is the date under the currentUId, which can be 1-3-2018, 2-3-2018 and so on. And as you can see, to achieve this, we needed to use nested queries.

Firebase querying data having childs as uid

Users is my root node, in that I have various childs created by getting the uid of the user who authenticated. Within these childs (uid) their are various childs like Info, General maintenance, Complaints.
I want to show some of the details like gCity, gProductModel from General maintenance child from all the users (uids). Each uid will have a different values of gCity, etc. Under General Maintenance, I want to retrieve all that data by setting it on an adapter and listview.
I am not able to retrieve it.
Database image
retreive code
When i am trying to retrieve data from the current user by using 'getuid()'(As shown below) ,its working fine . But when i try to retrieve all users data using' getkey()' , it isn't working .
user = FirebaseAuth.getInstance().getCurrentUser();
ref= FirebaseDatabase.getInstance().getReference("Users")
.child(user.getUid())
.child("General Maintenance");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(list_users.size() > 0)
list_users.clear();
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Generalperson user =
postSnapshot.getValue(Generalperson.class);
list_users.add(user);
}
if(list_users.size() != 0)
{ ListViewAdapter adapter = new
ListViewAdapter(ServicehistoryActivity.this, list_users);
list_data.setAdapter(adapter);}
else
{
list_data.setVisibility(View.INVISIBLE);
k.setVisibility(View.VISIBLE);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Please use this code:
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userId = ds.getKey();
DatabaseReference userIdRef = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.child("General Maintenence").getChildren()) {
String gCity = ds.child("gCity").getValue(String.class);
String gEmail = ds.child("gEmail").getValue(String.class);
String gPhone = ds.child("gPhone").getValue(String.class);
//and so on
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userIdRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
If you get this error, Listen at /Users failed: DatabaseError: Permission denied is because you are not authorized to the Database, check the Rules Tab in the Realtime database and make this change:
{
"rules": {
".read": true,
".write":true
}
}
Hope it helps.

Android Firebase Retrieve Realtime Data

I try to get data from the database.
Code:
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("User")mDatabaseUsers.orderByChild("name").startAt("m");
mDatabaseUsers.keepSynced(true); mDatabaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> map = dataSnapshot.getValue(Map.class);
String name = map.get("name");
Toast.makeText(AddFriendActivity.this,nick.toString(),Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to get all users with the letter m!
How does is work?
Thank you!
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
So you'll need to loop over the results in dataSnapshot:
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
System.out.println(userSnapshot.child("name").getValue(String.class));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
Also see the brief example in the Firebase documentation on using a value event to get a list of results.

How to delete records from a Firebase database when are existing multiple childs with Android?

I have a relative simple Firebase database which looks like this:
If i want to delete the Rooms node bases on the message, i know how to do it but how can i delete the Room1 node, when i only know the userName? When i'm using this code, nothing happens.
databaseReference = FirebaseDatabase.getInstance().getReference().child("Rooms");
Query deleteQuery = databaseReference.child("Room1").child("userModel").orderByChild("userName").equalTo("Alex");
deleteQuery.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ds.getRef().removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
If remodeling database is not an option, then the simplest way is to loop through Rooms node.
String username = "Alex";
FirebaseDatabase.getInstance().getReference().child("Rooms").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot room : dataSnapshot.getChildren()) {
boolean containUsername = false;
for (DataSnapshot roomChild : room.getChildren()) {
if (roomChild.child("userModel/userName").getValue(String.class).equals(username)) {
containUsername = true;
break;
}
}
if (containUsername)
FirebaseDatabase.getInstance().getReference("Rooms/" + room.getKey()).removeValue();
}
}
...
});
Note: I haven't check if roomChild.child("userModel/userName") can be used. If it produces error, change it with roomChild.child("userModel").child("userName")
I think it's better practice to return the ID using the getID() method and delete based on that.

Categories

Resources