Firestore事务在读取时失败

时间:2018-05-29 18:48:59

标签: ios swift firebase transactions google-cloud-firestore

在执行transactionfirestore时,我的初始读取操作使整个事务失败,并出现以下错误:“nilError”。

如果您首先不存在文档参考,那么如果您无法完成transaction,那就太好了吗?无法在transaction中的iOS文档中找到明确的答案。

我希望读取实际上是nil,其中文档引用当前不存在,随后执行setDataupdateData写操作。

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!")
}
}

0 个答案:

没有答案