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

后端 未结 1 329
孤城傲影
孤城傲影 2020-12-07 05:14

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

com.google.firebase.database.DatabaseExcep         


        
相关标签:
1条回答
  • 2020-12-07 05:36

    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);
    }
    
    0 讨论(0)
提交回复
热议问题