1

I have a database in Firebase for Android and I have an object with the attributes you see in the image. An object is stored in the database with the following code:

FirebaseUser user = mAuth.getCurrentUser();
String videoId = getIntent().getStringExtra("VIDEO_ID");
minuto = player.getCurrentTimeMillis();
Watching  watching  = new Watching(user.getUid(), videoId, String.valueOf(minuto));
DatabaseReference mRef = database.getReference().child("Watching").push();
mRef.setValue(watching);

The problem I have is as I am using push() to store the nodes I am having duplicate data as you can see in the image.

enter image description here

Is there any way to avoid storing duplicate data? Knowing that I don't have my own ID to store.

Any help ?

2
  • I think it depends under what conditions you decide to push, if I were you I will query by idUser and have list of all Watching items locally and before I decide to push I will check if idVideo exists in my list. Commented Sep 21, 2016 at 6:45
  • For a next question: you've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase Database console. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. Commented Sep 21, 2016 at 13:33

2 Answers 2

3

It depends on how you define a duplicate.

A common case where people have this question is when they're storing users. There the definition of a duplicate is simple: if two user objects have the same value for uid, they're the same user. So in that case you should store the users under their uid, instead of under a push ID.

The same applies for your situation. If a single user can only watch a single video, store the nodes under Watching by the uid of the user:

Watching
    "NX7...A33"
        "idVideo": "b13...o4s"
        "minute": "0"

But if it's the combination of uid + idVideo that is unique, store the nodes under a combined key:

Watching
    "NX7...A33_b13...o4s": "0"

Now you can easily prevent duplicates, by using setValue() instead of push():

String key = user.getUid() + "_" + videoId;
ref.child("Watching").child(key).setValue(String.valueOf(minuto));
Sign up to request clarification or add additional context in comments.

Comments

0

I was facing same problem like you and this is how i figure it out. I m checking email address that user entered with the one saved in my db.if any match found then show toast that email already exists otherwise save with new key.

mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                 //If email exists then toast shows else store the data on new key
                    if (!data.getValue(User.class).getEmail().equals(email)) {
                        mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email));
                    } else {
                        Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onCancelled(final DatabaseError databaseError) {
            }
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.