I have a database reference.
houseReference = FirebaseDatabase.getInstance().getReference("House");
I add an item to the db using:
houseReference.push().setValue(house);
Google firebase reference documentation says you can then get the key for the last item using
houseReference.getKey();
However, this returns House. What am I doing wrong?
Firebase has no concept of "the last key". But each DatabaseReference is a reference to a path in the database, and you get the last key in that path by calling getKey() on it. So if you call houseReference.getKey() you indeed get house, since that is the last key in the path that it refers to.
Now we get to the good stuff though.
When you call push() Firebase generates (in the client) a new unique key, and return a new DatabaseReference that point to that key under the current path. So houseReference.push() returns a new DatabaseReference to the new, unique, child location. And this means that you can call getKey() on this new DatabaseReference to get the last key of that new location.
So:
DatabaseReference newRef = houseReference.push();
System.out.println(newRef.getKey());
newRef.setValue(house);
Related
I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.
My users can create jobs. I want to add these jobs to my Firebase DB. I want the structure of the DB to look like this
- jobs
--uid
---name
---descritpion
---...
For the "uid", I'd like a random and unique string. Is it possible to generate it automatically somehow instead of generating a random number by myself?
FirebaseDatabase database = FirebaseDatabase.getInstance();
String key = database.getReference("jobs").push().getKey();
get the instance of firebase and get reference to child jobs then get the key. the key will be unique.
questions
-KkXXZkVBjaNwnnPtRzU
date:
"19052017"
question:
"Q9"
uid:
"WAX6aezrBzXuXW3RtnnSApMazRb2"
This is the database structure at the moment. I'm writing a code to delete the node based on the submitted key, in this case KkXXZkVBjaNwnnPtRzU. I've obtained the key using the getKey() method and now I want to use the removeValue() on an DatabaseReference object to which I'm passing the key as the reference.
How would I go about doing that?
I should mention that the current key value I'm passing is a string. Is something like this possible at all? Basically the same principle as passing the value to a child() method, but instead of accessing a child of the node, I'd go for its key.
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("questions");
myRef.child("KkXXZkVBjaNwnnPtRzU").removeValue();
It's simple as that. Of course, instead of hardcoding the values, you should use String which you've obtained previously.
I'm using a SortedList to sort my items inside the RecyclerView.
So I need to compare them by some unique Id, and Firebase provides awesome generated keys for that.
To push new value to a database I need to run:
public void addAdvert(Advert advert, String userId) {
String id = reference.child(NODE_USERS)
.child(userId)
.child(NODE_ITEMS)
.push()
.getKey() //execute this to just get the id
advert.setId(id);
getCurrentItemNode().child(advert.getId()).setValue(advert);
}
now my Advert item in database is fully equiped with Id and node key.
But I' curious, can I just write something like
reference.child(NODE_USERS)
.child(userId)
.child(NODE_ITEMS).push().setValue(advert);
And achieve the same result: both key and ID are set in database.
If you want to store the key in the item, you'll either have to perform two write operations or first generate the key and then write it.
But why do you want to store the key in the item to begin with?
As a side note, this code:
String id = reference.child(NODE_USERS)
.child(userId)
.child(NODE_ITEMS)
.push()
.getKey()
Is exactly the same as this:
String id = reference.push().getKey()
Firebase's push IDs are client-side generated, statistically guaranteed to be unique values. They're not dependent on the path and there is no round-trip to the database needed to determine these values.
See this blog post for more information: The 2^120 Ways to Ensure Unique Identifiers.
Scenario is : json object are uploaded from a rest api and to be accessed by an android app.
Isuue: at each upload, the object is stored under unique push id. This is variable and not known at receiver end. So complete path is not known.
In given part of code:
mRootRef = new Firebase("https://solvewa2y.firebaseio.com");
Firebase messagesRef=mRootRef.child(postId);
I need to replace postId with something so that I can get the object stored irrespective of the unique id OR a way to get the unique id each time
The answer is really simple. I struggled for a solution with eventlisteners until I found this answer here
//Create new reference calling push creates the unique key in firebase database but has no data yet
DatabaseReference mypostref = databaseRef.push();
//Use the new reference to add the data
mypostref.setValue(data);
//Call getKey on the ref - you must have 'key' in your data class
String mypostref.getKey());