ios - firebase - 在多个位置原子删除

标签 ios swift firebase firebase-realtime-database

有没有办法在 Firebase 中执行原子删除?

docs

You can also delete by specifying nil as the value for another write operation such as setValue or updateChildValues...Simultaneous updates made this way are atomic: either all updates succeed or all updates fail.

但我无法让它工作。我下面的代码返回错误

Cannot store object of type _SwiftValue at . Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray

我在这里遗漏了什么或者文档有误吗?

var topNodeRef = FIRDatabase.database().reference()
var childUpdates: [String:Any?] = [:]

childUpdates.updateValue(nil, forKey: "/owner")
childUpdates.updateValue(nil, forKey: "/pet")

topNodeRef.updateChildValues(childUpdates)

最佳答案

如果其他人遇到同样的问题,我设法通过使用 NSNull() 而不是 nil

来修复它
var topNodeRef = FIRDatabase.database().reference()
var childUpdates: [String:Any] = [:]

childUpdates["/owner"] = NSNull()
childUpdates["/pet"] = NSNull()

topNodeRef.updateChildValues(childUpdates)

关于ios - firebase - 在多个位置原子删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132001/

相关文章:

javascript - 如何获取 firebase firestore 生成的 id

ios - 如何检查每两张翻转卡片的数组中按钮的当前标题是否在卡片匹配游戏中匹配

javascript - UIWebView - 在网页上启用右键单击

swift - 未调用 preferredFocusEnvironments

javascript - 带有 iOS Swift 请求超时的 Express.js 服务器

reactjs - Typeorm + Firebase 函数 : "No connection options were found in any orm configuration files" after deployed

iphone - iOS CVImageBuffer 使用 AVCaptureSessionPresetPhoto 从 AVCaptureSessionDataOutput 变形

ios - 无法将类型 'A' 的值转换为 'B'

json - 将多个 JSON 与 id 组合

java - 升级 Android 版 Firebase 后,无法找到签名 (Lorg/shaded/apache/log4j/Logger;) 中引用的类