1

Having Firebase RealTime Database in Firebase application having web and android as client. Inserting values to the database by using push() method which generates random key and all the sub key value pairs are stored in the child by the web client. Now, I want to update some key value pairs of particular child by android. For example

KHSIHJKJBNJXKCB -
     name="xyz"
     age=19
JHGISFUSHFIJDSF -
     name="abc"
     age=20

How to update "age" attribute of particular key, by not using the key KJHUJHDFIUSH etc. just something like update age to 25 where name is "abc".

UPDATE

reference heirarchy

where "student 0" is replaced by some random values generated by Firabase. So I want to change the value "isCertificateReady" to false. How can I refer to the specific node while not knowing the key of the node. For this example we can use contact number as unique id.

The code which I used to solve the problem or get the things working on getting answer

coachingDatabaseReference.orderByChild("contactNumber")
    .equalTo(contactNumber)
    .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String key = dataSnapshot.getKey();

            coachingDatabaseReference.child(key)
                .child("isCertificateReady")
                .setValue(false);
        }

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

2 Answers 2

2

For that you have to use Query
Like in Android
myRef.orderByChild("age").equalTo("20").addListenerForSingleValueEvent(...
Query

then you can call for its key using datasnapshot.getKey() and then update the value
Update Specific Keys

6
  • I can not get the key. The Listener never gets any call. Because In fact there is no value change so the listener would not get any call. Dec 2, 2016 at 18:27
  • @DushyantSuthar are you even doing it correctly? There are tons of solution on stack to take a look on. Could you show your implementation with code.
    – Sahaj Rana
    Dec 2, 2016 at 18:31
  • @DushyantSuthar if there is no current value, a value listener will be invoked with an empty/null snapshot. If that doesn't work for you, share the minimal code that reproduces the problem. Dec 2, 2016 at 18:32
  • n it does get called for every single time you launch the activity or whatever that code is on.
    – Sahaj Rana
    Dec 2, 2016 at 18:32
  • @FrankvanPuffelen I have added some more things. I hope I would work. Thank you for considering it. Dec 2, 2016 at 18:44
0

Add some unique id to your student object if the name is not unique. If we assume the name is unique, then it is safe to run a transaction and update specific values inside as below.

myRef.orderByChild("name").equalTo("abc").runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        Student s = mutableData.getValue(Student.class);
        if (s == null) {
            return Transaction.success(mutableData);
        }

        s.setAge(20);
        // Set value and report transaction success
        mutableData.setValue(s);
        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean b,
                           DataSnapshot dataSnapshot) {
        // Transaction completed
        Log.d(TAG, "postTransaction:onComplete:" + databaseError);
    }
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.