本文介绍了如何将记录从某个位置复制到Firebase实时数据库中的另一个位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将Firebase.CompletionListener添加到ValueEventListener时出现此错误.

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)

这是我从此处获得的代码:

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) {

                                    }
                                });

当我不使用onCompleteListener时,应用程序运行正常,但崩溃了.

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

推荐答案

实际上,我回答了这个问题,但是当时我使用的API现在被认为是旧的.因此,我建议您使用以下方法:

Actually, I answered that question but at that time, I used an API that is considered now 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) {}
    };
    fromPath.addListenerForSingleValueEvent(valueEventListener);
}

这篇关于如何将记录从某个位置复制到Firebase实时数据库中的另一个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 07:46