将数据从一个节点移动或复制到 Firebase 数据库中的另一个节点 [英] Moving or copying data from one node to another in firebase database

查看:28
本文介绍了将数据从一个节点移动或复制到 Firebase 数据库中的另一个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我在一个节点上的数据(即,cart_details/UID)移动到另一个节点 orders/UID/order1.我尝试了不同的方法,但似乎都有些混乱.是否有任何内置功能或方法可以使工作更轻松?任何帮助表示赞赏.

I am trying to move my data present at one node i.e cart_details/UID to another node orders/UID/order1. I tried different ways of doing it but all seem to be a bit confusing. Is there any built-in functionality or method that could possibly make the job easier? Any help is appreciated.

我附上了同样的图片.图像.

I have attached the image for the same. IMAGE.

推荐答案

我建议你使用这个方法:

I recomand you to use this method:

public void copyRecord(Firebase fromPath, final Firebase toPath) {
    fromPath.addListenerForSingleValueEvent(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("Copy failed");
                    } else {
                        System.out.println("Success");
                    }
                }
            });
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {}
    });
}

这是一个副本,而不是您可能看到的移动,因此原件将保留在其原始位置.如果你想删除,你可以在 System.out.println("Success"); 之后的 fromPath 上使用 removeValue() 方法代码>.

This is a copy and not a move as you probably see, so the original will remain at his original place. If you would like to delete, you can use removeValue() method on the fromPath just after the System.out.println("Success");.

(2018 年 5 月 3 日).

这是使用新 Api 的代码.

Here is the code for using the new Api.

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 数据库中的另一个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆