Firebase实时数据库删除列表中的项目

huangapple go评论88阅读模式
英文:

Firebase Realtime Database delete item in List

问题

我在实时数据库中以这种格式存储了数据:

Firebase实时数据库删除列表中的项目

如果我想删除列表中的一个条目,名称为7P3MRvkC2FQkwcyC7CYVWZP9Ps72,我会使用这段代码。
此条目下面的id是自动生成的:

FirebaseDatabase.getInstance()
                .getReference()
                .child("users")
                .child(id) -------------------> 唯一id
                .orderByChild("orderId")
                .equalTo(orderId,"orderId")
                .getRef()
                .removeValue();

但这会删除完整的用户列表。

英文:

I have the data stored in the Realtime Database in this format:

Firebase实时数据库删除列表中的项目

If I wanted to delete one of the entries in the list named 7P3MRvkC2FQkwcyC7CYVWZP9Ps72, I use this code.
The id under this is generated automatically:

FirebaseDatabase.getInstance()
                .getReference()
                .child("users")
                .child(id) -------------------> unique id
                .orderByChild("orderId")
                .equalTo(orderId,"orderId")
                .getRef()
                .removeValue();

But this deletes the complete list of users.

答案1

得分: 1

尝试以下代码:

DatabaseReference ref = FirebaseDatabase.getInstance()
                .getReference()
                .child("users")
                .child(id);

ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        dataSnapshot.getRef().removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // 通知操作成功
            }
        });
    }
});
英文:

Try this code

DatabaseReference ref = FirebaseDatabase.getInstance()
            .getReference()
            .child(&quot;users&quot;)
            .child(id);

ref.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    dataSnapshot.getRef().removeValue().addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            // notify
                        }
                    });
                }

答案2

得分: 0

但是这将删除完整的用户列表

这是预期的行为如果您只想删除`orderId`属性保持特定orderId值的元素那么您应该像以下代码中的代码行一样迭代结果

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
DatabaseReference uidRef = usersRef.child(uid);
Query query = uidRef.orderByChild("orderId").equalTo(orderId);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            ds.getRef().removeValue();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //不要忽略潜在错误!
    }
};
query.addListenerForSingleValueEvent(valueEventListener);
英文:

> But this deletes the complete list of users.

That's the expected behavior. If you need to remove only the elements where the orderId property holds the value of a particular "orderId", then you should iterate trough the results like in the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child(&quot;users&quot;);
DatabaseReference uidRef = usersRef.child(uid);
Query query = uidRef.orderByChild(&quot;orderId&quot;).equalTo(orderId);
ValueEventListener valueEventListener = new ValueEventListener() {
	@Override
	public void onDataChange(DataSnapshot dataSnapshot) {
		for(DataSnapshot ds : dataSnapshot.getChildren()) {
			ds.getRef().removeValue();
		}
	}

	@Override
	public void onCancelled(@NonNull DatabaseError databaseError) {
		Log.d(&quot;TAG&quot;, databaseError.getMessage()); //Don&#39;t ignore potential errors!
	}
};
query.addListenerForSingleValueEvent(valueEventListener);

huangapple
  • 本文由 发表于 2020年9月7日 00:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63766425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定