I am a bit new to Firebase and so have been playing around with to help myself get more acquainted with it. So while I was playing around with realtime databases, I was trying to append data to the JSON tree. The code is as below
mSaudi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
count++;
mHistory = mChildRef.child(Integer.toString(count));
current = riyadh;
mChildRef.setValue(riyadh);
mHistory.push().setValue("riyadh");
}
});
The tree which I require is something like this:
value:
1: some text
2: some other text
But what's a actually happening is this:
value:
1: some text
and on updation
value:
2:some text
the previous entry gets erased
I have tried changing the references in various ways but to no avail. Any help in this regard would be appreciated.
If you would like to save both values, you have to save them using a variable such as a Hashmap. If you save a string and then try save another one under the same branch, it will delete everything previously saved. So try the following
HashMap<String, String> map = new HashMap<>();
map.put("1","String");
map.put("2","String");
mHistory.push().setValue(map);
This will save both the strings without deleting one.
If you would only like to add one String
mHistory.push().child("1").setValue("Your first String");
The biggest problem with this though is that everytime you use push() you generate a random key, so you would have to save the key as a string and use it as a reference in your child.
When you set a value on Firebase, it is going to replace everything in, and under the reference.
Let's say that you have a house value, with 2 childs: Color and Size.
If you want to edit only the color value, before the setValue(), you will have to change the reference you are pushing to.
If your reference was getReference().child("houses") and you push something there, it's going to replace everything there and below it. The way to do it is create a new reference (or update the previews one) like this: getReference().child("houses").child(houseKey).child("color") and push your String there.
In your example, you will need to add the field you want to change as a child before the push() method.
The other way was already told by #Janwilx72 and is getting the whole object, updating the value locally and pushing the entire object again.
You can try this
mChildRef.child("2").setValue("some text");
It should be appending new item instead of overwriting them
Related
FireStore Array
I already retrieve the vaccine name and the number of count but the problem is the SPACE is also replace or being removed.
Retrived
I WANT TO OUTPUT THE ITEM LIKE THIS
WITH SPACE SAME VALUE IN THE FIRE STORE
I ALREADY GET IT USING THIS CODE BELOW IT I HOPE YOU GET THE POINT OF THE CODE BELOW CLICK THE LINKED TEXT TO SEE IT
IMAGE FOR THE CODE
Basically I used the Character.isDigit, isAlphabetic, isWhiteSpace
then save it to stringbuffer. then save it to my textview
I assigned my buffer variable for text to get also the whitespace.
Using Flutter I have a form that contains an AutoCompleteTextField, I am trying to get changing of the value of the text field working. The value to be edited is placed into the AutoCompleteTextField when the form is opened using the technique linked to my previous question:
How to set the initial value of Flutter autocomplete_textfield
Now I find that if I try to change the value of the field the autocompletion appears to work, but the new text does not appear in the form data when the form is submitted, the previous entry remains. I have tried adding the new text to the form data but this does not change the behavior.
The "itemSubmitted" code looks like:
itemSubmitted: (species) {
setState(() {
searchTextField.textField.controller.text = species.commonName;
});
The submit method calls validate and save on the form data:
if (_formKey.currentState.validate() == false) {
return;
}
_formKey.currentState.save();
What more do I need to do to get the new selection in the AutoCOmpleteTextField updated in the firm data?
Update:
It appears that if I start with a blank form I am able to search for an entry in the AutoCompleteTextField, save the data to my database (without clearing the form), search for a new value, and the data is correctly placed into the form data each time. This suggests that the initialization of the field when the form is opened is causing the issue.
Sid
This issue was entirely of my own making, I am using the same code to both create and edit an entry. To differentiate between new and edit I was testing for valid object being passed to the page, my mistake was that when my text search field was updated I was simply checking if there was a valid object had been passed to the page, of course, the new data was in the form data structure and was never being placed into the object being edited.
The solution was to simply first check if there is a valid input object, if there is then a secondary test is made to see if the form data matched the input object, if not the new data is then placed into the input object.
I want to append two different hash map pairs on a particular node one with key value both being String and other String and Array List. But problem is Later one is replacing first one in Fire base database. what should i do? I cant use push() all the time.Link of my code. Please help
You can achieve this if you generate a unique random key. This can be done using push() method.
As we know, in the case of HashMap, it replaces the old value with the new one.
Don't forget to use updatechildren() method, and not setValue() method directly on the refrence when you want to update data.
Hope it helps.
Just use 1 Map<String, Object> instead of 2 different ones.
then you put all the data in that single map, string, arraylist
I have a realtive simple Firebase database, in which i have 2 models. A ListModel and a UserModel. In my Lists, i'm using push() method to generate unique ids. Each unique id i want to be added as a key and "true" as it's value under Users/gmail#gmail,com/Lists.
When i add the first list, the database looks like this:
And everything works fine, but when i try to add another one, the database looks like this:
In Users/gmail#gmail,com/Lists, the first one is overwritten by the second insert. How can i add the specific id and the specific value, as a new item as shown below?
And this is my code:
final UserModel um = new UserModel();
um.setUserEmail("gmail#gmail,com");
userDatabaseReference.setValue(um);
ListModel lm = new ListModel();
lm.setListName(listName);
listKeyDatabaseReference = listDatabaseReference.push();
listKey = listKeyDatabaseReference.getKey();
listKeyDatabaseReference.setValue(lm);
listDatabaseReference.child(listKey).child("Users").child("gmail#gmail,com").setValue("true");
userDatabaseReference.child("gmail#gmail,com").child("Lists").child(listKey).setValue("true");
Thanks in advance!
Check the official doc:
For basic write operations, you can use setValue() to save data to a specified reference, replacing any existing data at that path.
Your problem is here:
userDatabaseReference.setValue(um);
In this way you are overriding all children in the userDatabaseReference path.
It means that the first record in Users/gmail#gmail,com/Lists is just deleted when you are adding the second one.
Before using the
userDatabaseReference.setValue(um);
you can check if the record exists.
If doesn't exist use the setValue to add the user-model with all its data.
If it exists, just skip this step and add the data in the lists path inside the same user.
Since a ParseRelation can have a huge number of row, is there a performant way to get the number of these rows?
For a big size I cannot simply query all the list and get the size.
In the Parse SDK documentation, it is not recommended to use query. countInBackground when there is more than 1000 objects.
So how can I query this size?
[EDIT] : potential issue if I increment dedicated counter for the ParseRelation:
Let's say on android I display a list of items with a button to click "addToRelation".
This button should be visible only if the relation is not already done. This means I need first to check on each item if they belong to the relation.
Then, when the user click on several buttons I call for each the backend method to add the relation and increments the counter. (This already make a lot of Parse request).
Now suppose because of some bad cache synchronization the button "addToRelation" is enabled while the relation already exist for this item.
If I call the method:
obj.add("relations",relation);
// increment the relations count by using the increment function
obj.increment("relationsCount");
obj.saveInBackground(. . .)
The method will not crash (I tested that if you add twice the same relation nothing happens)
but the counter will be incremented +1!
To avoid this I need to check twice on each item if they are not already in relation. This create too much redundant remote requests. So how to avoid this ?
Another issue may a
happen when I use saveEventuallyand the method silently fail; so if the User repeat several times the action, the counter will be incremented/decrimented several times in the local cache for the "saveEventually". If for any reason the method save succed finally the counter value will be wrong !!
what you can do is to create additional fields with the name of realtionCount (or something else according to your relation name). This field will be integer and then each time you create a new object you can increment this field using the increment option. So your code should look like the following:
// create new relation
final ParseObject relation = new ParseObject("your_relation_class_name");
relation.put("{RELATION_KEY_FIELD_NAME}","{RELATION_FIELD_VALUE}");
// add the relation to the parent object
obj.add("relations",relation);
// increment the relations count by using the increment function
obj.increment("relationsCount");
obj.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null){
// object saved!
}
}
});