I want to save data to four document in firestore. But I want to cancel all saving if in one of docs has error. I dont know it is possible with async-await. I share with you giveOrder function and button function that I call the giveOrder function in it. Finally ss is I got the error. May you help me
public func giveOrder(description: String, total: String, orderModel: [ChosenProduct]) async throws -> Bool {
let dealerID = orderModel[0].dealerID
let orderID = UUID().uuidString
getAccountID { email, customerID in
let orderInfo: [String : Any] = ["Date": Date.now,
"Dealer ID": dealerID,
"Customer ID": customerID,
"Statu": "Preparing",
"Order ID": orderID,
"Description": description,
"Total Cost": total]
self.reference = self.database.document("Orders/\(customerID)/\(orderID)/Order Info")
self.thirdReference = self.database.document("Orders/\(dealerID)/\(orderID)/Order Info")
Task {
try await self.reference.setData(orderInfo, merge: true)
for product in orderModel {
let orderPost: [String : Any] = ["Name" : product.name,
"Store Name" : product.storeName,
"Unit": product.unit,
"Unit Type": product.unitType,
"Price": product.price,
"Price Type": product.priceType,
"Product ID": product.id,
"Dealer ID": product.dealerID]
self.secondReference = self.database.document("Orders/\(customerID)/\(orderID)/\(product.id)")
try await self.secondReference.setData(orderPost, merge: true)
}
try await self.thirdReference.setData(orderInfo, merge: true)
for product in orderModel {
let orderPost: [String : Any] = ["Name" : product.name,
"Store Name" : product.storeName,
"Unit": product.unit,
"Unit Type": product.unitType,
"Price": product.price,
"Price Type": product.priceType,
"Product ID": product.id,
"Customer ID": customerID]
self.fourthReference = self.database.document("Orders/\(dealerID)/\(orderID)/\(product.id)")
try await self.fourthReference.setData(orderPost, merge: true)
}
}
}
return true
}
This is button function:
@objc func tappedBuyButton() async {
let comment = commentText.text ?? "No comments"
let total = totalLabel.text
let ordered = NewOrderVC.chosenProducts
do {
let success = try await DatabaseManager.shared.giveOrder(description: comment, total: total!, orderModel: ordered)
if success {
NewOrderVC.chosenProducts.removeAll(keepingCapacity: false)
self.tableView.reloadData()
} else {
self.makeAlert(title: "Error", message: "Could not order. Please try again!")
}
} catch {
self.makeAlert(title: "Error", message: "Could not order. Please try again!")
}
}
And this is the thread I got: [error: memory read failed for 0x2b88f7db5e00
Thread 1: EXC_BAD_ACCESS (code=1, address=0x2b88f7db5e90)]1