简体   繁体   中英

Delete all Data belonging to user

I am rebuilding a social media app like Instagram.

My user can decide to delete their account and then I would like to automatically delete all things belonging to the user.

Most of the stuff I can delete easily, but things like posts is where I struggle because the uid is just a sub child of the key where the post is saved into.

My Database tree:

 "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" : "user3@mail.de",
  "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"
}

My function where I delete data

    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")

        }
    })
}

I would love to iterate through all posts and look for every post which contains the uid of the current user and then delete these posts.

Thanks in advance :)

I would build a server task that can run this asynchronously, removing the responsibility from the mobile device. If the app crashes or loses connection, you'll get a severely broken data set. You need to be able to run it in a stable and verifiable environment. This inherently solves your problem as well, since now you can (at your server's leisure) cycle through and delete all posts owned by the user.

As far as your app is concerned, you'll just need to call a single API after validating that the user wants to delete their account, and voila.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM