删除不存在的密钥时,Firebase数据库不会返回错误 [英] Firebase Database doesn't return error when removing a nonexistent key
问题描述
在数据库
中,我删除了一个有效的实际密钥 -LoGeKWGQMJ2EZ5b_Scp
.但是随后,我尝试再次删除相同的完全相同的密钥,并打印出已成功删除.然后,我添加了一个完全伪造的密钥 12345 ,它仍然打印成功删除
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
}
然后我尝试了一次原子删除,看看会发生什么,并打印出成功删除,其中包含1个真实密钥和1个伪密钥.即使删除了真实密钥并再次尝试后,它仍然会为伪密钥和不存在的密钥打印成功删除:
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.
代码:
// 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
})
顺便说一句,使用 Storage
,如果成功删除一次并尝试再次删除它,它将返回不存在
错误.
Btw, with Storage
if you successfully delete something once and try to delete it again, it will return a does not exist
error.
为什么数据库
不会为不存在/先前删除的密钥和/或伪造密钥返回错误,而 Storage
确实会返回错误?
Why is Database
not returning an error for a nonexistent/previously deleted key and/or a fake key but Storage
does return an error?
推荐答案
当操作完成后,如果密钥不存在,则Firebase实时数据库认为删除操作成功.当前操作是否实际删除了密钥并不影响密钥成功与否,因此您所看到的是预期的行为.
The Firebase Realtime Database considers a delete operation successful when the key doesn't exist after the operation completes. 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.
Cloud Firestore遵循对不存在的文档执行删除操作的相同逻辑,因此看来Cloud Storage在这里很奇怪.据我所知,没有记录在案的造成这种差异的原因.
Cloud Firestore follows the same logic for delete operations on non-existing documents, so it seems Cloud Storage is the odd one out here. There is no documented reason for this difference, as far as I know.
这篇关于删除不存在的密钥时,Firebase数据库不会返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!