在执行transaction
到firestore
时,我的初始读取操作使整个事务失败,并出现以下错误:“nilError”。
如果您首先不存在文档参考,那么如果您无法完成transaction
,那就太好了吗?无法在transaction
中的iOS
文档中找到明确的答案。
我希望读取实际上是nil,其中文档引用当前不存在,随后执行setData
与updateData
写操作。
var db = Firestore.firestore()
db.runTransaction({ (transaction, errorPointer) -> Any? in
var document: DocumentSnapshot
do {
try document = transaction.getDocument(self.db.document("path/nestedPath"))
} catch let fetchError as NSError {
//Errors here.
errorPointer?.pointee = fetchError
return nil
}
if let documentData = document.data() {
transaction.updateData([x: y], forDocument: db.document("path/nestedPath"))
}
else {
transaction.setData([x : z], forDocument: db.document("path/nestedPath"))
}
return nil
})
{ (object, error) in
if let error = error {
print("Transcation Completion Error: \(error)")
} else {
print("Transaction Succeeded!")
}
}