我想知道是否有办法避免在删除值时调用onDataChange
。
我想删除节点内的所有值,但无法完全删除该节点,因为我在 onDataChange
中设置了它。
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!(dataSnapshot.haschild(aValue))
//add aValue to the child
if (dataSnapshot...
因此节点被删除后会立即重新创建。
此外,即使节点已被删除,onDataChange
内的语句也会被调用,从而导致应用程序崩溃。
目前,我设置 boolean
来检查节点是否被删除并且它是否有效,但我想知道是否有更好的方法来做到这一点。
最佳答案
I'm wondering if there is a way to avoid calling onDataChange when removing a value.
不,没有。根据官方文档关于ValueEventListener的onDataChange()方法:
This method will be called with a snapshot of the data at this location. It will also be called each time that data changes.
也就是说,即使是写入、更新甚至删除操作,都会触发该方法。因此,每次数据库发生更改时,onDataChange()
都会触发,不幸的是您无法避免这种情况。
关于java - Firebase 实时数据库 : Is there a way to avoid calling onDataChange when a value is removed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52982026/