In the Database I deleted an actual key -LoGeKWGQMJ2EZ5b_Scp which worked fine. But then I tried to delete the same exact key again and it printed successfully deleted. I then added a completely fake key 12345 and it still printed successfully deleted
guard let userId = Auth.auth().currentUser?.uid else { return }
let fakeKey = "12345"
let postsRef = Database.database().reference().child("posts").child(userId).child(fakeKey)
postsRef.removeValue { (error, _) in
if let error = error {
print(error.localizedDescription)
return
}
print("successfully deleted") // always prints
}
I then tried an atomic delete to see what would happen and it printed successfully deleted for 1 real key and 1 fake key. Even after the real key was deleted and I tried again it still printed successfully deleted for both a fake key and a nonexistent key:
guard let userId = Auth.auth().currentUser?.uid else { return }
let fakeKey = "12345"
let postsRef = "/posts/\(userId)/\(fakeKey)"
// this is a real key
let realKey = "-LvXTCmXfpzJA9SUBX8U"
let postsUserIdsRef = "/posts_userIds/\(userId)/\(realKey)"
var dict = [String: Any]()
dict.updateValue(NSNull(), forKey: postsRef)
dict.updateValue(NSNull(), forKey: postsUserIdsRef)
let rootRef = Database.database().reference()
rootRef.updateChildValues(dict, withCompletionBlock: { (error, _) in
if let error = error {
print(error.localizedDescription)
return
}
print("successfully deleted") // always prints
})
The odd thing is when I tried to do the same thing with storage I always get a does not exist error, which is what I expect
Object myUserId/-MHRkqMuc9c43MDNnbkw/-LoGeKWGQMJ2EZ5b_Scp.mp4 does not exist.
code:
// for this example postId is a real key but the videoId is the nonexistent key from above
guard let userId = Auth.auth().currentUser?.uid else { return }
let realPostId = "-MHRkqMuc9c43MDNnbkw"
let nonexistentVideoId = "-LoGeKWGQMJ2EZ5b_Scp"
let storageRef = Storage.storage().reference().child(userId).child(realPostId).child("\(nonexistentVideoId).mp4")
storageRef.delete(completion: { (error) in
if let error = error {
print(error.localizedDescription) // prints Object myUserId/-MHRkqMuc9c43MDNnbkw/-LoGeKWGQMJ2EZ5b_Scp.mp4 does not exist.
return
}
print("successfully deleted") // never prints
})
Btw, with Storage if you successfully delete something once and try to delete it again, it will return a does not exist error.
Why is Database not returning an error for a nonexistent/previously deleted key and/or a fake key but Storage does return an error?