Mattwalk
Mattwalk

Reputation: 139

How can I create a unique key and use it to send data in Firebase?

I'm using Firebase in my project and I was trying to create a unique key using Firebase. Using this key I wanna send posts when user start the activity. Like this:

 "Posts"
     |
      -> "Parent Unique key"
                 |
                 -> "child unique key 1"
                 -> "child unique key 2"
                 -> "child unique key 3"
                 ...

I wanna create Parent Unique key and by using it I wanna send posts. I know how to create unique key in firebase using push() but the problem is when user restart the activity, a new unique key will generate. I don't wanna create the same parent key again after creating once and I can't use user_id here as multiple users have different ids. Also I don't wanna store the parent key in some storage medium. Is it possible to create such key in firebase?

Upvotes: 4

Views: 10721

Answers (3)

Srushti
Srushti

Reputation: 131

You can create a unique key in Firebase database using push() and then add child nodes under that key. Now next time when you come to that activity, first check if parent node exists or not. If the node exists, save parent node key and use it to save new child nodes.

    String strParentKey;
        DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("Posts");
                    mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot != null && dataSnapshot.exists()) {
                                // parent node exists, get parent node from datasnapshot
                              strParentKey = dataSnapshot.value().toString();
                            } else {
                               // parent node doesn't exists, generate parent node using push
                               strParentKey = FirebaseDatabase.getInstance().getReference().child("Posts").push().getKey();
                            }
                        }

                        @Override
                        public void onCancelled(final DatabaseError pDatabaseError) {
                            pOnChildCheckListener.onChildDoesntExist();
                        }
                    });

Now using the parentKey, you can add child node using that reference.

String childKey = FirebaseDatabase.getInstance().getReference("Posts").child(strParentKey).push().getKey();

Hope this will help you lead to the solution.

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80952

If you do this inside onCreate() method:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Posts").push();

then yes, everytime you enter that activity a new push id will be created. This is because push() generates a random id.

To be able to solve this, you need to add push() later on.. or under a condition but not at the beginning.

So you can add it onClick of a button:

 ref.push().child("name").setValue(name);

This way everytime you click a button it will generate a push id, not when you restart the activity.

Upvotes: 2

Alex Mamo
Alex Mamo

Reputation: 1

To solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String uniqueKey = rootRef.child("Posts").push().getKey();
DatabaseReference uniqueKeyRef = rootRef.child("Posts").child(uniqueKey);

So for adding data, use only uniqueKeyRef reference. So, using this code you'll create a unique id only once. You'll be able to add those childs under a single id.

If you want another key, see UUID which can help you generate unique keys for your Firebase database without having to use the push() method.

Upvotes: 2

Related Questions