删除所有属于用户的数据

问题描述:

我正在重建像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每一个岗位和然后删除这些帖子。

感谢提前:)

我会建立一个可以异步运行此,去除移动设备的责任的服务器任务。如果应用程序崩溃或失去连接,则会收到严重损坏的数据集。您需要能够在稳定和可验证的环境中运行它。这也固有地解决了您的问题,因为现在您可以(在您的服务器闲置时)循环并删除用户拥有的所有帖子。

就您的应用而言,您只需在验证用户想要删除其帐户之后调用单个API即可。

+0

因为我是初学者,所以我不知道该怎么做^^ –

+0

有很多材料可以开始使用,但你必须从某处开始。阅读与Firebase服务器端交互的内容。 – brandonscript