【发布时间】:2018-08-01 13:35:30
【问题描述】:
我有一个简单的 Firestore 事务,它首先读取以验证文档是否存在,如果不存在,则执行一些写入操作。
代码 sn-p 看起来像这样
Firestore.firestore().runTransaction({ (transaction, errorPointer) -> Void in
let snapshot: DocumentSnapshot
// First check if user already exists.
let userRef = firestore.document("users/\(user.uid)")
do {
try snapshot = transaction.getDocument(userRef)
} catch let fetchError as NSError {
errorPointer?.pointee = fetchError
return
}
if !snapshot.exists {
// Document doesn't exist. Start write operation...
...
当文档不存在时,getDocument 似乎没有返回有效的快照,而是引发了错误。我不想依靠错误来断定文档不存在(因为它可能会与其他错误混淆)。
在 Firestore 事务中验证文档是否存在的正确方法是什么?
【问题讨论】:
标签: swift firebase transactions google-cloud-firestore