How to update value of a particular child node in firebase realtime database? - android

I'm making a basic android app project, I have this database structure and want to update a specific child node (lets say room_price)
I've tried doing this but looks like I'm missing something which is preventing me from achieving this.
DatabaseReference dbRef=FirebaseDatabase.getInstance().getReference();
dbRef.child("Rooms")
.child(roomNumber)
.child("room_price")
.setValue(updatedPrice);
roomNumber and updatedPrice are variables having int values.
When I execute this code, instead of updating the value of room_price it deletes the room_price node from the tree structure and I haven't been able to get why it does that. Please correct me if something is wrong with the code, or suggest some other way of doing this.
Please note that it has no issue with database rules. its either the code or the database path that causes this problem.

To update room_price, try the following:
DatabaseReference dbRef=FirebaseDatabase.getInstance().getReference();
dbRef.child("Rooms").child(roomNumber);
Map<String, Object> updates = new HashMap<>();
updates.put("room_price", updatedPrice);
dbRef.updateChildren(updates);
more info here:
Update Field

Please note that Firebase database does not accept null values, so if you try to update a node with anything that results in null value, the node will be deleted. For more information, please check the documentation on https://firebase.google.com/docs/database/admin/save-data
I'm not sure if that is the reason behind your issue, I would need more information on what updatedPrice is, but try to log this variable and examine its contents, maybe it is resolving as a null value and causing the error.
Good luck!

Related

Firebase deep query Android

I'm trying to find a user by a value in an array. This is the structure of a user:
And I'm using this but it returns null:
FirebaseDatabase.getInstance().getReference().child("users")
.orderByChild("tokens")
.equalTo(from)
UPDATE:
I've tried Puff suggestion and this is my structure now:
The code I'm using is:
FirebaseDatabase.getInstance().getReference().child("users)
.orderByKey()
.equalTo(token)
But still I get null result.
UPDATE 2:
So what I'm trying to do is not possible. You can't create a query starting from parent and give in the child of child. As Puff said you can ONLY query direct reference, witch is not very useful in all cases. Still I managed to get what I wanted saving the value in another form, locally.
:(

How to write an object with an ArrayList<> attribute to Firebase database (Android)?

I am trying to write a Spot object to my Firebase database. All was good an well until I added an ArrayList<> attribute to the Spot class. Now I can no longer write Spot object to the database and when I try, my app just.. stops. No crash, so I don't get any error feedback in the log :/
Here is my writing code:
public void writeSpot(Spot spot, AddSpotContract.RequiredModelOps subscriber) {
Log.i("DBGR", "writing spot");
getReference().child(spot.getId()).setValue(spot);
Log.i("DBGR", "what is going on??");//if Spot object has an ArrayList<Uri> attribute, this is never reached
subscriber.onSpotWritten();
}
So does anybody know why I can't write an object with an ArrayList<> attribute to the database? I've tried with an array, but that isn't supported (at least in that case I get the feedback to tell me that).
Thanks a lot :)
EDIT: I found the answer.
It actually had nothing to do with the ArrayList, the issue came from trying to write a Uri to the database, as it isn't a supported class (see error "java.lang.StackOverflowError:" when try to save object in Firebase Database)
In order to write data objects to your Firebase database, in stead of using a List, use a Map. If you have also in your pojo an Map field, change it to a Map.
Hope it helps.

ParseObject.remove not working in Back4App

I am triying to delete a field of an object in Back4App, but I cannot achieve such a simple operation. By "deleting" I mean set a field that has data to "undefined".
According to the guide, I just have to call myObject.remove("field"). I tryed that (with correct field name), then save the object (I tried all of the saving functions available), but the object is unmodified. There is no error thrown.
I can change the field (with put ("field", otherObject), because it is a pointer field) with no problem. But put("field", JSONObject.NULL) is not working either.
I do not know if this code would work in the original Parse, I am coding this now. The equivalent function in iOS ([myObject removeObjectForKey:#"field"];) in the same database is working nicely...
For what I could gather from your question, the problem is that you're trying to clean a field from a relational object:
"I can change the field (with put ("field", otherObject), because it is
a pointer field)"
On that case, I'm not really sure if using simple object deletion would work. I'd suggest you to take a look at Parse's documentation on Relational Data in order to understand how you should remove that field.
Long story short, I'm not sure if the idea of cleaning the field that you wish will work, but what can be done when you have a relation like this:
ParseUser user = ParseUser.getCurrentUser();
ParseRelation<ParseObject> relation = user.getRelation("field");
relation.add(MyObject);
user.saveInBackground();
Is to simply remove the relation like this:
relation.remove(MyObject);
As you can check in the link above.

Can't set user objectId to parse pointer field

For a new project I was running some tests. For now I have a function in which I am trying to save an object to the parse.com framework and all went well untill I added a user pointer object. The pointer line seems to block my code when I use the current user's objectId as a value for it. Using a random word of the user's email adres however does work. I added my code below.
// Create a data object and store it online.
public void createDataObject(){
ParseObject object = new ParseObject("TestData");
object.put("action", "trial");
object.put("value", "succes?");
object.put("name", ParseUser.getCurrentUser().getUsername());
String pointer = ParseUser.getCurrentUser().getObjectId();
Log.i(TAG, pointer);
object.put("pointer", ParseObject.createWithoutData("_User", pointer));
object.saveEventually();
}
I hope anyone can help me figure it out, help is greatly appreciated!
Update: The problem is not my pointer command but it's the fact I use saveEventually(); If I use saveInBackground it just works but I want to use saveEventually because of possible lost network connection. Does anyone have a clue what could be the problem?
Update 2: Finally decided to delete the app and the installationId of the Parse data browser. Upon reinstalling the app everything started working like it should. I probably had a bad piece of code that got stuck with my installation Id. I hope others with the same problem reach this post fast and don't spend several days searching for an answer!
Update 3: There was a certain function that tried to find a user based on a pointer with the app user's object ID which made the app crash once and than totally unusable. I marked the first answer as the correct answer since it solved my original question but just fyi.. there was more going on than I expected at first.
Just try this:
public void createDataObject(){
ParseObject object = new ParseObject("TestData");
object.put("action", "trial");
object.put("value", "succes?");
object.put("name", ParseUser.getCurrentUser().getUsername());
object.put("pointer", ParseUser.getCurrentUser());
object.saveEventually();
}
No need to get the object ID to set the current User.
Edit:
You can also refer https://stackoverflow.com/a/28403968/2571060 to set pointer
I had a similar issue with the iOS SDK. Are you using 'automatic user creation' ? Cause in that case, ParseUser.getCurrentUser() returns a User object whose objectId is null. According the documentation, it is null until the user or any object related to it is saved. But in my experience, it is not the case, I had to save the user first or I would get an error like 'User can only be created during signUp' or something like this.

How to remove specific value from Firebase database? I want to remove table13 from “Occupied” and transfer it to “Available”

I want to Delete Specific value from Firebase Database following is Database which i have stored
To remove specific child nodes in Firebase you need to call set value null on that particular node Or you can call databaseReference.removeValue();
Try below code it will solve your problem
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("table").child("occupied");
databaseReference.setValue(null);
Update:
You are using a dataSnapshot try this
dataSnapshot.getRef().setValue(null);
You have to do that in a few steps:
Load in device memory data which you want to remove.
Remove from firebase data which you already loaded.
Write data from memory to new firebase database node.
But is not a best solution... here you must handle a few marginal cases like application interruption on step 2. Of course you can handle that but here is a lot of code which must be written and maintained. I suggest you to use firebase cloud funtions. To put that 3 steps to cloud functions and just to handle results in firebase realtime database listeners, just 1 call to cloud functions to obtain amazing results :)
Also in firebase cloud functions you can put a listener to your "available" node which will be triggered when some node data will be removed. In that listener you can get that data which must be removed and move it to "occupied" node. From my point of view that solution is a little bit simpler.

Resources