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());
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.
I am new to Android programming. I am working on an app. When I signup in my app this shows the previously added data and does not ask me to add new user data to the database.
Even if I again login with a registered user then it again shows the previously added data.
I want that when I create a new user then a new node should create and then its child with data.
Database Picture
First of all, you need unique id to share your data in firebase. You can use user id for that. If you use firebase authentication. you can get user id from
FirebaseAuth.getInstance().getCurrentUser().getUid();
You can use this with email password firebase authentication. Likewise, you can get a unique id for authentication. Then u need to use that id as a key of your firebase database. Then your data structure should be changed like below. (key would be userids)
childsQuerity
|-------------adminInfo
|-------key1 -> value1
|-------asas1234 -> value2
|-------------childInfo
|-------key1 -> value1
|-------key2 -> value2
in your android activity use below command to push data to db
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("childInfo").child(userid);
ref.setValue(userDataUWant);
When creating a new user, get userid and update that firebase data like above-mentioned method. Then if an already registered user logged to the system. u can get user id using above mentioned method and you can get that user details as well.
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.
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.
I have Firebase data that looks like this.
I want to create a Query object in java that retrieves only Invites (those are the objects with randomly generated key's KHcYy...) which have a name = "John Smith".
Here is the code for my current attempt, which is only giving me an empty query:
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.child("brother").equalTo("John Smith");
I tried taking a look through this tutorial on queries, but that only really had info on doing sorty and ranking based queries. I couldn't find any info on this type of task, and the API Reference didn't really make it clear what I'm supposed to be inputting in any of these calls.
Thanks for the help!
Have you tried this...
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.orderByChild("brother").equalTo("John Smith");