如何从Firebase数据库中删除用户

时间:2017-06-11 09:39:58

标签: android firebase design-patterns firebase-realtime-database

所以我想为用户添加功能,将他的帐户删除到我的应用..简单吧?我查看文档,它很容易:

FirebaseAuth.getInstance().getCurrentUser().delete()

但是,他在FirebaseDatabase和Storage中的所有数据仍然存在。这是我的结构(给予或采取) enter image description here

在firebase存储中它有点简单,所以我不包含在这个问题中..

现在问题是,用户以某种方式与几乎所有节点相关联,并且他需要删除很多地方。从android设备用户发送“DeleteUserRequest”到服务器,服务器负责删除用户。但经过制作方法,它的地狱!我需要借用爸爸的监视器对其进行编码,因为这样的深层嵌套调用!

这是方法

public static void deleteUser(DeleteUserRequest request, ExecutorService executorService) {
    deleteUserReferences(request.getUserId(), executorService);
    deleteUserStorage(request.getUserId());
}

/**
 * Delete Tree
 * - /grades/userId
 * - /subjects/userId
 * - /user_groups/userId -> save groupIdList
 * - /group_members/groupId/userId
 * - /groups/groupId/ [where createdBy=userId] <- Delete that Group using GroupRequestManager
 * - /user_friends/userId -> save friendIdList
 * - /user-friends/friendId/userId
 * - /user_tokens/userId
 * - /users/userId
 * - /friend_requests/userId
 * - /friend_requests/friendUserId/userId
 */
private static void deleteUserReferences(final String userId, ExecutorService executorService) {
    final MultiPathUpdate deleteUpdate = new MultiPathUpdate(FirebaseDatabase.getInstance().getReference());
    FirebaseDatabase.getInstance().getReference()
            .child(DatabaseConstants.LOCATION_GROUPS)
            .orderByChild(DatabaseConstants.PROPERTY_CREATED_BY)
            .equalTo(userId)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final ArrayList<String> groupIdsWhereAdmin = new ArrayList<>();
                    if (dataSnapshot.exists()) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            groupIdsWhereAdmin.add(snapshot.getKey());
                        }
                    }
                    for (String groupId : groupIdsWhereAdmin) {
                        GroupRequestManager.deleteGroup(new DeleteGroupRequest(groupId, userId), executorService);
                    }
                    FirebaseDatabase.getInstance().getReference()
                            .child(DatabaseConstants.LOCATION_USER_GROUPS)
                            .child(userId)
                            .addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    if (dataSnapshot.exists()) {
                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                            final String groupId = snapshot.getKey();
                                            deleteUpdate.delete(new ChildLocation()
                                                    .child(DatabaseConstants.LOCATION_GROUP_MEMBERS)
                                                    .child(groupId)
                                                    .child(userId));
                                        }
                                    }
                                    FirebaseDatabase.getInstance().getReference()
                                            .child(DatabaseConstants.LOCATION_USER_FRIENDS)
                                            .child(userId)
                                            .addListenerForSingleValueEvent(new ValueEventListener() {
                                                @Override
                                                public void onDataChange(DataSnapshot dataSnapshot) {
                                                    if (dataSnapshot.exists()) {
                                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                                            final String friendId = snapshot.getKey();
                                                            deleteUpdate.delete(new ChildLocation()
                                                                    .child(DatabaseConstants.LOCATION_USER_FRIENDS)
                                                                    .child(friendId)
                                                                    .child(userId));
                                                        }
                                                    }
                                                    FirebaseDatabase.getInstance().getReference()
                                                            .child(DatabaseConstants.LOCATION_FRIEND_REQUEST)
                                                            .orderByChild(userId)
                                                            .equalTo(true)
                                                            .addListenerForSingleValueEvent(new ValueEventListener() {
                                                                @Override
                                                                public void onDataChange(DataSnapshot dataSnapshot) {
                                                                    if (dataSnapshot.exists()) {
                                                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                                                            final String friendRequestId = snapshot.getKey();
                                                                            deleteUpdate.delete(new ChildLocation()
                                                                                    .child(DatabaseConstants.LOCATION_FRIEND_REQUEST)
                                                                                    .child(friendRequestId)
                                                                                    .child(userId));
                                                                        }
                                                                    }
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_USER_GROUPS)
                                                                            .child(userId));
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_GRADES)
                                                                            .child(userId));
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_SUBJECTS)
                                                                            .child(userId));
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_USER_FRIENDS)
                                                                            .child(userId));
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_USER_TOKENS)
                                                                            .child(userId));
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_USERS)
                                                                            .child(userId));
                                                                    deleteUpdate.delete(new ChildLocation()
                                                                            .child(DatabaseConstants.LOCATION_FRIEND_REQUEST)
                                                                            .child(userId));
                                                                    deleteUpdate.update(new DatabaseReference.CompletionListener() {
                                                                        @Override
                                                                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                                                            if (databaseError != null) {
                                                                                databaseError.toException().printStackTrace();
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                                @Override
                                                                public void onCancelled(DatabaseError databaseError) {
                                                                    databaseError.toException().printStackTrace();
                                                                }
                                                            });
                                                }
                                                @Override
                                                public void onCancelled(DatabaseError databaseError) {
                                                    databaseError.toException().printStackTrace();
                                                }
                                            });

                                }
                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                    databaseError.toException().printStackTrace();
                                }
                            });
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    databaseError.toException().printStackTrace();
                }
            });
}

正如你所看到的那样,调试和维护它的地狱......我没有包括GroupRequestManager.deleteGroup(..)但是信任,它和这种方法一样的地狱......现在的问题是我该怎么办?是否存在某种扁平嵌套调用框架?它很棘手,因为需要进行所有这些调用才能获得正确的数据。什么有助于解决这个问题?另一种编程风格? RxJava?我切换到kotlin吗?我只是不给用户删除他的帐户的选项,而只是“禁用”它或什么?

1 个答案:

答案 0 :(得分:0)

正如道格史蒂文森所说,并不比在所有地点创建用户数据更糟糕。实际上更简单。为您的位置创建一个set the value to null的方法,如下所示:

private static void deleteUser() {
    Map<String, Object> map = new HashMap<>();
    map.put("referenceOne", new HashMap<>().put("keyOne", null));
    map.put("referenceTwo", new HashMap<>().put(keyTwo, null));
    map.put("referenceThree", new HashMap<>().put(keyThree, null));
    //and so on
    databaseReference.updateChildren(map);
}

这是执行此操作的最佳方式,因为您一次删除所有数据。它也被称为原子事务,因为通过这种方式,您可以确保所有这些代码行都将被执行。

希望它有所帮助。