仅在设置了数据后如何调用完成处理程序?

时间:2019-03-27 16:16:40

标签: ios swift google-cloud-firestore

在Firebase中设置数据之前,下面看到的函数似乎通过完成处理程序传递orderId,这将导致使用orderId从Firestore检索关联数据的后端函数失败。后端函数可能由于其他原因而失败,但这可能是因为该函数在传递orderId时尚未完成将数据设置为firestore的操作。我该如何做才能使我的函数仅在设置数据后才传递orderId?

public func uploadTransactionData(_ menuItems: [MenuItem], tip: Int, tax: Int, rewardAmountApplied: Int, totalPaidFromCredit: Int, discountAmount: Int, subTotal: Int, balanceId: String, locationId: String, completion: @escaping ((String?) -> ())) {
    guard let userId = Auth.auth().currentUser?.uid else { completion(nil); return }
    let utilitiesManager = UtilitiesManager()
    let timestamp = utilitiesManager.timestamp()
    var listOfItems = [Any]()
    for item in menuItems {
        do {
            let jsonData = try JSONEncoder().encode(item)
            let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
            listOfItems.append(jsonObject)
        }
        catch let err {
            print("$-- error uploading transaction data \(err)")
            completion(nil)
            return
        }
    }
    let orderRef = Firestore.firestore().collection("order").document()
    let orderId = orderRef.documentID
    let totalPrice = subTotal + tax + tip + discountAmount
    let params: [String: Any] = ["date": timestamp,
                                "total_amount": totalPrice,
                                "tip_amount": tip,
                                "tax_amount":tax,
                                "discount_amount": discountAmount,
                                "reward_amount": rewardAmountApplied,
                                "balance_amount": totalPaidFromCredit,
                                "balance_id": balanceId,
                                "subtotal": subTotal,
                                "account_id": userId,
                                "location_id": locationId,
                                "status": "PENDING",
                                "notes": "",
                                "line_items": listOfItems
    ]
    orderRef.setData(params)
    { err in
        if let e = err {
            print("$-- error uploading transaction data \(e)")
            completion(nil)
        } else {
            completion(nil)
        }
    }
    completion(orderId)
}

1 个答案:

答案 0 :(得分:1)

setData是异步的,并在数据库生成任何结果之前立即返回。在结果完成之前,您无条件地在函数末尾调用了completion()。也许您是想这样说:

orderRef.setData(params)
{ err in
    if let e = err {
        print("$-- error uploading transaction data \(e)")
        completion(nil)
    } else {
        // Change this line to yield the order id to the callback on success
        completion(orderId)
    }
}
// Remove this line that always calls the completion before the result
// completion(orderId)