2017-06-04 125 views
1

我正在重建像Instagram這樣的社交媒體應用。刪除所有屬於用戶的數據

我的用戶可以決定刪除他們的賬戶,然後我想自動刪除屬於用戶的所有東西。

大多數東西我可以很容易地刪除,但像帖子這樣的事情是我奮鬥的地方,因爲uid只是保存帖子的關鍵的子子節點。

我的數據庫樹:

"Feed" : { 
"es5fIbnKFpX4szcCbroUqHjJg6E3" : { 
    "-KjTBFFE5QzktG1IT5u0" : true, 
    "-KjTHFNe1RRS8Ly6bKsA" : true, 
    "-KjY30xwWA2IJBwlvyzf" : true 
} 
"myPosts" : { 
"jlkRoaucY6Q4GBkzhor5yAAl97I2" : { 
    "-KjTBFFE5QzktG1IT5u0" : true, 
    "-KjTHFNe1RRS8Ly6bKsA" : true, 
    "-KjY30xwWA2IJBwlvyzf" : true 
} 
"posts" : { 
"-KjTBFFE5QzktG1IT5u0" : { 
    "bookmarkCount" : 0, 
    "caption" : "Toll", 
    "commentCount" : 1, 
    "creationDate" : 1.494081403379004E9, 
    "hoursSinceUpload" : 0, 
    "likeCount" : 0, 
    "photoUrl" : "https://firebasestorage.googleapis.com/v0/b/funcloud-8e84e.appspot.com/o/Posts%2F76192CBE-55F0-4907-889A-849E196D5796?alt=media&token=de675609-4b73-411d-b402-f1ff3db64f79", 
    "ratio" : 1.502732240437158, 
    "score" : 16.38698994684219, 
    "uid" : "jlkRoaucY6Q4GBkzhor5yAAl97I2" 
}, 
"-KjTHFNe1RRS8Ly6bKsA" : { 
    "bookmarkCount" : 1, 
    "bookmarks" : { 
    "jlkRoaucY6Q4GBkzhor5yAAl97I2" : true 
    }, 
    "caption" : "Traumhaft", 
    "commentCount" : 0, 
    "creationDate" : 1.494082976550228E9, 
    "hoursSinceUpload" : 0, 
    "likeCount" : 2, 
    "likes" : { 
    "es5fIbnKFpX4szcCbroUqHjJg6E3" : true, 
    "jlkRoaucY6Q4GBkzhor5yAAl97I2" : true 
    }, 
    "photoUrl" : "https://firebasestorage.googleapis.com/v0/b/funcloud-8e84e.appspot.com/o/Posts%2F306BF7E1-9FEF-493A-ABF8-C0E061E8648F?alt=media&token=128bdd90-023a-49ac-8361-19c02c631183", 
    "ratio" : 1.502732240437158, 
    "score" : 166.6491847103437, 
    "uid" : "jlkRoaucY6Q4GBkzhor5yAAl97I2" 
} 
"users" : { 
"es5fIbnKFpX4szcCbroUqHjJg6E3" : { 
    "email" : "[email protected]", 
    "profilText" : "Schreib etwas über dich", 
    "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/funcloud-8e84e.appspot.com/o/profile_image%2Fes5fIbnKFpX4szcCbroUqHjJg6E3?alt=media&token=ce8d8722-39bc-457a-8149-e51c837ef0a3", 
    "username" : "Blondine", 
    "username_lowercase" : "blondine" 
} 

我的功能,我刪除數據

static func removeUserData() { 



    let user = Auth.auth().currentUser 

    let uid = API.User.CURRENT_USER?.uid 



    Database.database().reference().child("users").child(uid!).removeValue() 
    Database.database().reference().child("Feed").child(uid!).removeValue() 
    Database.database().reference().child("Favoriten").child(uid!).removeValue() 
    Database.database().reference().child("LikesFromUsers").child(uid!).removeValue() 
    Database.database().reference().child("post-comments").child(uid!).removeValue() 
    Database.database().reference().child("notification").child(uid!).removeValue() 
    Database.database().reference().child("followers").child(uid!).removeValue() 
    Database.database().reference().child("following").child(uid!).removeValue() 
Database.database().reference().child("LikesCommentsFromUsers").child(uid!).removeValue() 
Database.database().reference().child("comments").child(uid!).removeValue() 




    user?.delete(completion: { (error) in 
     if let error = error { 
      print(error.localizedDescription) 
     } else { 
      print("success") 

     } 
    }) 
} 

我很想通過所有文章進行迭代,並查找包含當前用戶的UID每一個崗位和然後刪除這些帖子。

感謝提前:)

回答

2

我會建立一個可以異步運行此,去除移動設備的責任的服務器任務。如果應用程序崩潰或失去連接,則會收到嚴重損壞的數據集。您需要能夠在穩定和可驗證的環境中運行它。這也固有地解決了您的問題,因爲現在您可以(在您的服務器閒置時)循環並刪除用戶擁有的所有帖子。

就您的應用而言,您只需在驗證用戶想要刪除其帳戶之後調用單個API即可。

+0

因爲我是初學者,所以我不知道該怎麼做^^ –

+0

有很多材料可以開始使用,但你必須從某處開始。閱讀與Firebase服務器端交互的內容。 – brandonscript

相關問題