在Android上的Firebase实时数据库中移动数据(复制然后删除) [英] Move data in Firebase Realtime Database on Android (copy and then delete)
问题描述
我正在尝试将数据从实时数据库中的一条路径移动到另一条路径.因此,我必须将数据从一个路径(1)复制到另一路径(2),然后再从(1)中删除数据.
I'm trying to MOVE data from one path in Real-time Database to another one. So I must copy the data from one path (1) to another path (2) and after that remove the data from (1).
我尝试了以下代码:
ConfiguracaoFirebase.getFirebaseReferenceRelatorios().limitToFirst(20)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (final DataSnapshot ids: dataSnapshot.getChildren()){
String ano = ids.child("ano").getValue().toString();
String mes = ids.child("mes").getValue().toString();
String dia = ids.child("dia").getValue().toString();
String nomePaciente = ids.child("nomePaciente").getValue().toString();
moveFirebaseRecord(ids.getRef(), ConfiguracaoFirebase.getFirebaseReferenceReports().child(nomePaciente).child(ano).child(mes).child(dia).child(ids.getKey()));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
和此功能moveFirebaseRecord:
and this func moveFirebaseRecord:
public void moveFirebaseRecord(final DatabaseReference fromPath, final DatabaseReference toPath) {
fromPath.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
toPath.setValue(dataSnapshot.getValue(), new DatabaseReference.CompletionListener() {
@Override
public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
fromPath.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Constant.print("REMOVED: " + fromPath.getKey());
}
});
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
但是,当我运行时,我看到它将fromPath正确地移动到toPath,但是紧接在它之后删除了刚创建的toPath ...因此它正在删除toPath和fromPath ...有人知道为什么会发生这种情况,以及如何从Firebase剪切和移动数据?预先感谢.
However when I run I see that it moves the fromPath to toPath correctly but right after it DELETES the toPath, which was just created... So it's deleting the toPath and fromPath... Anybody got an idea why does it happen and how can I cut and move the data from Firebase? Thanks in advance.
推荐答案
如果 toPath
最初似乎已经被写入后被删除,则通常是由安全规则引起的.因此,请检查用户是否真的有权写入该路径.
If the toPath
is deleted after it initially seems to have been written, that is typically caused by security rules. So check if the user really has permission to write to the path.
通常,我强烈建议将新值的写入和现有值的删除合并到单个多位置更新中,如下所示:
In general I'd highly recommend combining the write of the new value and the remove of the existing value into a single multi-location update like this:
public void onDataChange(DataSnapshot dataSnapshot)
{
Map<String, Object> updates = new HashMap<String, Object>();
updates["/path/to/new/value"] = dataSnapshot.getValue();
updates["/path/to/old/value"] = null;
rootRef.updateChildren(updates).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Constant.print("REMOVED: " + fromPath.getKey());
}
});
});
}
这样,要么两个操作都成功,要么都不执行(例如,如果安全规则拒绝另一个操作).
That way either both operations succeed, or neither of them is executed (e.g. if security rules reject one of the other).
这篇关于在Android上的Firebase实时数据库中移动数据(复制然后删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!