简体   繁体   中英

How to copy a record from a location to another in Firebase realtime database?

I get this error when I try to add Firebase.CompletionListener to my ValueEventListener .

com.google.firebase.database.DatabaseException: Failed to parse node with class class c.kristofer.jaxx.MainActivity.MainActivity$2$1$1
   at com.google.android.gms.internal.firebase_database.zzjd.zza(Unknown Source)
   at com.google.android.gms.internal.firebase_database.zzjg.zzc(Unknown Source)
   at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
   at c.kristofer.jax2.MainActivity.MainActivity$2$1.onDataChange(MainActivity.java:110)
   at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
   at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
   at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6682)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

And this is my code that I got from here :

fromPath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(uid)
                         .child(uid);
toPath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(deeplink.getQueryParameter("groupUid"))
                         .child(uid);
userIdPath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(deeplink.getQueryParameter("groupUid"))
                         .child(uid)
                         .child("uid");
deletePath = FirebaseDatabase.getInstance()
                         .getReference()
                         .child("Groups")
                         .child(uid);
fromPath.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        toPath.setValue(dataSnapshot.getValue(), new Firebase.CompletionListener() {
                                            @Override
                                            public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                                                if (firebaseError != null) {
                                                    System.out.println("Data could not be saved. " + firebaseError.getMessage());
                                                } else {
                                                    System.out.println("Data saved successfully.");
                                                    userIdPath.setValue(uid);
                                                    deletePath.removeValue();
                                                }
                                            }
                                        });
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

When I don't use the onCompleteListener the app works fine but when I do it crashes.

Actually, I answered that question, but at that time, I used a version of Firebase Android SDK that is now considered to be old. So for that, I recommend you to use the following method:

private void moveRecord(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) {
            Log.d(TAG, databaseError.getMessage()); //Don't ignore potential errors!
        }
    };
    fromPath.addListenerForSingleValueEvent(valueEventListener);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM