Restore firebase object by copying some android data

I am using firebase in my application. I have a requirement when I need to change the key value for one object. Please refer to the below image as a link to my firebase database.

enter image description here

For DCu1, I need to change my key value to something else, let's say DCu4. Now for this I create another cloned DCu1 object with the changed key value as DCu4, after which I will delete DCu1. The problem is related to nested parameters such as DeviceList, Status, Request. How do I copy these values ​​into a new cloned object? I need your help. Thanks in advance.

+3


source to share


1 answer


When you read data into node, all attached files are loaded.

You can use the following code:



 FirebaseDatabase.getInstance().getReference().child("/path/to/DCu1").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            FirebaseDatabase.getInstance().getReference().child("path/to/DCu4").setValue(dataSnapshot.getValue());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

      

I hope this helps

+3


source







All Articles