Move Data In Firebase Realtime Database On Android (copy And Then Delete)
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). I
Solution 1:
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:
publicvoidonDataChange(DataSnapshot dataSnapshot)
{
Map<String, Object> updates = newHashMap<String, Object>();
updates["/path/to/new/value"] = dataSnapshot.getValue();
updates["/path/to/old/value"] = null;
rootRef.updateChildren(updates).addOnSuccessListener(newOnSuccessListener<Void>() {
@OverridepublicvoidonSuccess(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).
Post a Comment for "Move Data In Firebase Realtime Database On Android (copy And Then Delete)"