Vahe Gharibyan
Vahe Gharibyan

Reputation: 5683

How to use addValueEventListener in a first time for Android

It's very confusing Firebase Database api for Android.

I would like to update some part of json objects, but I can't. This is firebase database.

enter image description here

First of all the user create vocabulary, following this code

    /* reference of /vocabulary/{user_id}/*/
    final DatabaseReference vocabularyRef = FBDatabaseReference.getAllVocabulary(user);

    if (!TextUtils.isEmpty(oldName)){
        // if vocabulary renamed remove preview
        vocabularyRef.child(oldName).removeValue();
    }
    // set user vocabulary
    vocabularyRef.child(String.valueOf(userVocabulary.name())).
            setValue(new FBVocabulary(userVocabulary), completionListener);

If vocabulary had created successfully user can add words into them. But after adding words, user can modify vocabulary name or color etc.. In that time I lose all words, because the vocabulary object has recreate.

According of Firebase doc for updating a child I should addListenerForSingleValueEvent or addValueEventListener When I use addListenerForSingleValueEvent instead of previews code, User can not create vocabulary.

In firebase documentation written if reference not created yet the snapshot objects is null.

    /* reference of /vocabulary/{user_id}/vocabulary3/ */
    final DatabaseReference vocabularyRef = FBDatabaseReference.getAllVocabulary(user).child("vocabulary3");
    vocabularyRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Object value = dataSnapshot.getValue();
            if (value == null){
                dataSnapshot.getRef().setValue(new FBVocabulary(userVocabulary), completionListener);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

In this case each time called onCancelled(DatabaseError databaseError) with exception: Permission denied. As far as I understand is is because the reference is null.

  1. Can I check if reference exist or not ? then update data.

  2. How can I use addValueEventListener in a first time ?

Upvotes: 2

Views: 3438

Answers (2)

Vahe Gharibyan
Vahe Gharibyan

Reputation: 5683

I found the solution, That is because of database rules. depend of this line

".read"  : "auth != null && $uid == auth.uid && data.exists()"

if data.exists() rule added each time addValueEventListener->onCancelled method called while data have not created yet.

While without data.exists() rule, addValueEventListener returned object snapshot and it's null. Then I can create if is null, or update if is not null

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

Try this:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("vocabulary1");
ref.child("color").setValue(new_value);
ref.child("id").setValue(new_value);
ref.child("footercolor").setValue(new_value);
ref.child("headercolor").setValue(new_value);
//and so on..

You can do this, thus updating the values under the node without changing the value of the word node.

To check if it exists or not do this:

DatabaseReference refs=FirebaseDatabase.getInstance().getReference().child("vocabulary").child("key_here").child("vocabulary1");
 refs.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               if(dataSnapshot.exists()){
                 //dont do anything
                }
                else{
                     refs.child("color").setValue(new_value);
                      //so on
                    } 
                  }
               });

Upvotes: 2

Related Questions