0

Im making a social media app. All user data is child of username object. For example

root:
  +users:
    +ricksanchez:
      +name:"Rick Sanchez";
      +email:"rick@sanchez.com";
      +bio:"I'm super smart";
      +followers:
        +follower1:morty;
        +follower2:summer;
      ....

How to change ricksanchez key without losing data?

Is it possible to rename a key in the Firebase Realtime Database?

I checked out this question but im quite new in coding.I guess thats not Java.Do you know how to do it in Java?

uzaysan
  • 583
  • 1
  • 4
  • 18
  • Possible duplicate of [Is it possible to rename a key in the Firebase Realtime Database?](https://stackoverflow.com/questions/39107274/is-it-possible-to-rename-a-key-in-the-firebase-realtime-database) – Diyako Sep 03 '18 at 11:25
  • Im quite new in coding.I didnt understand what it says.Do you know how to do it in Java? – uzaysan Sep 03 '18 at 11:33
  • Keys cannot be renamed in Firebase. This is a very good example why keys should be unrelated to the data they contain. See the answer to [this question](https://stackoverflow.com/questions/41386373/compare-textfield-text-to-firebase-string-swift/41387240#41387240) for more details. – Jay Sep 05 '18 at 17:26

1 Answers1

0

You cannot change the name of a key, let's say ricksanchez to something else. There is no API for doing that. If you want to change the name of a key, you definitely need to copy that particular object to another location, change the name and then delete the old record. But to somply change the name of key is not possible in Firebase.

Edit: According to your comment, please use the following method to copy a record from a location to another.

private void copyRecord(DatabaseReference fromPath, final DatabaseReference toPath) {
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            toPath.setValue(dataSnapshot.getValue()).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isComplete()) {
                        Log.d(TAG, "Success!");
                    } else {
                        Log.d(TAG, "Copy failed!");
                    }
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    fromPath.addListenerForSingleValueEvent(valueEventListener);
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Do you know how to copy all data. I mean I have a user class i can keep simple infos such as name, email, or password but username key also includes followers ,images and other stuff. How can i copy all of these and upload to another username? – uzaysan Sep 03 '18 at 11:24
  • This is basically another question but I have updated my answer with your second demand. So please see my updated answer. – Alex Mamo Sep 03 '18 at 11:41
  • Hey I'm using your code and it works like a charm but when i run your code second time without closing app my app crash.Do you have any idea why? – uzaysan Sep 06 '18 at 20:15
  • Please post another fresh question including the entire error, so me and other users can help you. – Alex Mamo Sep 07 '18 at 07:58