删除对象时删除嵌套字段中的索引 [英] Remove indexes that are in nested field when deleting an object
问题描述
我有一个像下面这样的结构:
我还在Firebase上,这次我有一个关于删除对象的问题。
users:{
UsErId1:{
name:Jack,
email:m @ il
,
UsErId2:{+},
UsErId3:{+}
},
user_contacts:{
UsErId1:{
UsErId2:true,
UsErId3:true
,
UsErId2:{
UsErId1:true
}
}
$ c $所以如果我想要删除一个用户,我必须:
$ ol $ b $
$ b b
删除用户对象
删除user_contacts分支下的用户对象
删除指向该用户的user_contacts的所有索引
我的性能问题来自于第3点,因为我需要迭代所有user_contacts条目以查看它存在于孩子。
一个例子代码如下:
pre prerivate $ {
firebaseDatabase.getReference( 用户)子(用户ID).removeValue()。
firebaseDatabase.getReference(users_contacts)。child(userId).removeValue(); (DataSnapshot usersSnapshot:dataSnapshot.getChildren)
public void onDataChange(DataSnapshot dataSnapshot){
)
firebaseDatabase.getReference(users_contacts)。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
()){
for(DataSnapshot contactSnapshot:usersSnapshot.getChildren()){
String contactId = contactSnapshot.getValue(String.class);
if(contactId.equals(userId)){
contactSnapshot.getRef()。removeValue();
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
}
我想到了两种可能的解决方案:
-
解决方案1 :不要删除
user_contacts
,当我加载用户联系人时,我必须调用每个用户来查看用户是否为空(已被删除),在这种情况下,不要显示它。顺便说一句,这会导致数据库变脏。 解决方案2 :创建一个反向索引 - Delete the user object
- Delete the user object under the user_contacts branch
- Remove all the indexes from user_contacts that are pointing to that user
Solution 1: Don't delete the indexes from
user_contacts
and when I've to load the user contacts, I've to do a call to each user to see if the user is null (has been deleted), and in that case, don't show it. By the way, this results in a dirty database.Solution 2: create a reverse index
contacts_users
where I store the users for which the user I'm trying to delete is referenced. As follow:contacts_user: { UsErId1: { UsErId2: true }, UsErId2: { UsErId1: true }, UsErId3: { UsErId1: true } }
contacts_users
我在哪里存储用户,我尝试删除的用户被引用。如下所示: contacts_user:{
UsErId1:{
UsErId2:true
},
UsErId2:{
UsErId1:true
},
UsErId3:{
UsErId1:true
}
}
<
$ b因此,当我要删除一个用户的时候,我会看看它孩子在 contacts_users
中,并且知道在联系人中拥有它的每个用户,并继续删除它(现在我可以知道整个路径)。这似乎是一个更类似NoSql的方法。
您怎么看?还有另一种方法吗?
解决方案您的第二个解决方案是我会建议如何做,不需要搜索。你可以将这些信息存储在每个用户的基础上,但是如果它变得太大,在其他地方有更好的。
同样,在另一个方向删除也变得更容易。
p>
I'm still on Firebase and this time I have a question related on the deletion of objects.
I have a structure like the following:
users: {
UsErId1:{
name: "Jack",
email: "m@i.l"
},
UsErId2: { + },
UsErId3: { + }
},
user_contacts: {
UsErId1:{
UsErId2: true,
UsErId3: true
},
UsErId2: {
UsErId1: true
}
}
So if I want to delete an user I have to:
My performance problems comes from the point 3, because I need to iterate all the user_contacts entries to see if a user it's present in the childrens.
An example of code is the following:
private void deleteUser(String userId) {
firebaseDatabase.getReference("users").child(userId).removeValue();
firebaseDatabase.getReference("users_contacts").child(userId).removeValue();
firebaseDatabase.getReference("users_contacts").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot usersSnapshot : dataSnapshot.getChildren()) {
for( DataSnapshot contactSnapshot : usersSnapshot.getChildren() ){
String contactId = contactSnapshot.getValue(String.class);
if( contactId.equals(userId) ){
contactSnapshot.getRef().removeValue();
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I've thought of two possible solutions:
So, when I have to delete a user, I will look at its childs in contacts_users
and know every users that has it in its contacts, and proceed to delete it (now that I can know the entire path). This seems to me to be a more NoSql-like approach.
What do you think? Is there another way to do it?
解决方案 Your second solution is how I would suggest doing it, no need to search. You could store that information on a per user basis, but if it grows too large having it elsewhere is better.
Likewise deleting in the other direction also becomes easier.
这篇关于删除对象时删除嵌套字段中的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!