3

I am using this code to add new item to Firebase on Android. However, if somebody tried to add another item with the same key, it will replace the current item! How can I prevent this from happening? I mean like regular database; I want to use key as my primary key and not allow any item with same key to be added (or to replace another existing item)

myFirebaseRef.child(key).setValue(item, new Firebase.CompletionListener() 
{
    @Override
    public void onComplete(FirebaseError firebaseError, Firebase firebase)
    {

    }
});

I don't want to use push() though because the keys have meaning in my app and I want to maintain there uniqueness

DigitalPerson
  • 191
  • 3
  • 12
  • See http://stackoverflow.com/questions/25294478/how-do-you-prevent-duplicate-user-properties-in-firebase. While that answer is for JavaScript, the approach is the same for any Firebase SDK – Frank van Puffelen Apr 06 '16 at 14:38

3 Answers3

1

i think, you can use a primary key by define yourself, or check exist key before add value by method:

Firebase fb = myFirebaseRef.child(key);
if(fb == null){
    //it means the child is not exist
}
else{
    //it means the child is existing
}
atimetoremember
  • 133
  • 3
  • 12
1

Use runTransaction which is an atomic operation to make sure there are no overriding of keys. You can refer to this answer on how to use runTransaction. Below is a sample answer

firebaseRef.child("uniquekeys").child(uniqueKey).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            if (mutableData.getValue() == null) {
                mutableData.setValue(value);
                return Transaction.success(mutableData);
            }

            return Transaction.abort();
        }

        @Override
        public void onComplete(FirebaseError firebaseError, boolean commited, DataSnapshot dataSnapshot) {
            if (commited) {
                // unique key saved
            } else {
                // unique key already exists
            }
        }
    });
Community
  • 1
  • 1
Viven
  • 627
  • 7
  • 14
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11916720) – piotrek1543 Apr 06 '16 at 17:54
  • Still quite new to StackOverflow. I've edited with an example – Viven Apr 07 '16 at 01:34
1

Try it.

myFirebaseRef.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.getValue() == null) {
                //does not exists
                } else {
                //exists
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
Rodrigo Silva
  • 463
  • 3
  • 10