簡體   English   中英

如何從Firebase數據庫中刪除用戶

[英]How to delete a User from Firebase Database

所以我想為用戶添加功能以將其帳戶刪除到我的應用中。 我看一下文檔,它很簡單:

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

但是,他在FirebaseDatabase和Storage中的所有數據仍然保留。 這是我的結構(給予或接受) 在此處輸入圖片說明

在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(..) ...我沒有包括GroupRequestManager.deleteGroup(..)但值得信賴,它與這種方法一樣..現在的問題是我該怎么辦? 是否有某種扁平化的嵌套調用框架? 這很棘手,因為需要進行所有這些調用才能獲取正確的數據。什么能幫助解決這一難題? 另一種編程風格? RxJava? 我會改用Kotlin嗎? 我是否只是不讓用戶選擇刪除其帳戶,而是只是“禁用”它或其他?

正如道格·史蒂文森(Doug Stevenson)所說,這不比在所有位置創建用戶數據都差。 其實比較簡單。 創建一個方法, set the value to null您所在位置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);
}

這是最好的方法,因為您將立即刪除所有數據。 這也稱為原子事務,因為通過這種方式,您可以確保所有此代碼行都將被執行。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM