Create a unique ID for my child key in Firebase - android

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.

Related

Not able to use getRef() of Firebase in android

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.

Creating a new user and also add new data to the firebase database at every SignUp

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.

Firebase query for multiple where condition

I need equivalent query of
SELECT PlayerName FROM GameScores
WHERE Department = 'HR' OR Department = 'IT' OR Department = 'Marketing'
In Firebase for android , I don't want multiple call, just one request and get all data that match with where clause key. How I can do it?
The only way to filter a property on multiple values in Firebase is if those values are in a range. For example, you could get all items matching your values with:
ref.child("GameScores")
.orderByChild("Department")
.startAt("HR")
.endAt("Marketing")
The problem with this is that it also matches scores from other departments whose name is between HR and Marketing, such as
There is no way in either the Firebase Realtime Database or Cloud Firestore to pass multiple separate values into a query. You will have to execute a separate query for each value and merge them client-side.
Also see:
firebase equivalent to sql where in ()

Preset object Id when pushing to Firebase

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.

Obtaining the Unique Push ID in Firebase

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());

Categories

Resources