[英]Firebase Database doesn't return error when removing a nonexistent key
In the Database
I deleted an actual key -LoGeKWGQMJ2EZ5b_Scp
which worked fine.在
Database
中,我删除了一个实际的密钥-LoGeKWGQMJ2EZ5b_Scp
,它运行良好。 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
然后我添加了一个完全伪造的密钥12345 ,它仍然打印成功删除
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.然后我尝试了一个原子删除,看看会发生什么,它打印成功删除了 1 个真实密钥和 1 个假密钥。 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奇怪的是,当我尝试对存储做同样的事情时,我总是得到一个
does not exist
的错误,这正是我所期望的
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.顺便说
Storage
,如果你成功删除了一些东西并再次尝试删除它,它会返回一个does not exist
的错误。
Why is Database
not returning an error for a nonexistent/previously deleted key and/or a fake key but Storage
does return an error?为什么
Database
不为不存在/先前删除的密钥和/或假密钥返回错误,但Storage
确实返回错误?
The Firebase Realtime Database considers a delete operation successful when the key doesn't exist after the operation completes. Firebase 实时数据库认为操作完成后键不存在时删除操作成功。 Whether the current operation actually deleted the key is not a factor in its success, so what you're seeing is the intended behavior.
当前操作是否实际删除了密钥并不是其成功的因素,因此您看到的是预期的行为。
If you want to have an operation that only succeeds when the key was present initially and you then actively removed it, you will need to use a transaction .如果您希望只有当密钥最初存在然后您主动删除它时才会成功的操作,您将需要使用 transaction 。
Cloud Firestore follows the same logic for delete operations on non-existing documents, so it seems Cloud Storage is the odd one out here. Cloud Firestore 遵循相同的逻辑对不存在的文档进行删除操作,因此 Cloud Storage 似乎是这里的一个奇怪的地方。 There is no documented reason for this difference, as far as I know.
据我所知,这种差异没有记录在案的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.