【发布时间】:2019-12-27 21:33:40
【问题描述】:
我正在尝试通过这样的 for 循环删除所有购物车项目:
func removeRelatedProductFromUserCart(selectArrayNodeIds: [String], completion: @escaping(Error?)->()) {
let semaphore = DispatchSemaphore(value: 0)
let serialQueue = DispatchQueue(label: "com.queue.Serial")
serialQueue.async {
for nodeId in selectArrayNodeIds {
FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList: false, item: nodeId) { (error) in
completion(error)
semaphore.signal()
}
semaphore.wait()
}
}
}
和 Firebasemanager:
func removeProductFromUsersWishOrCartList(isForWishList: Bool, item: String?, completion: @escaping (Error?) -> ()) {
let uid = UserDefaults.standard.string(forKey: "uid")!
if isForWishList == true {
Firestore.firestore().collection("user").document(uid).updateData([
"wishList": FieldValue.arrayRemove([item!])
]) { (error) in
completion(error)
}
} else {
Firestore.firestore().collection("user").document(uid).updateData([
"cart": FieldValue.arrayRemove([item!])
]) { (error) in
completion(error)
}
}
}
然后,当我尝试从同一字段中获取购物车商品时,我会在延迟后获取更新后的购物车商品列表。我观察到打开firebase控制台的删除过程,我发现删除在控制台中发生了延迟。但是错误响应(error == nill) 并没有等到removeRelatedProductFromUserCart 中。那么我怎样才能等到删除 Firestore 上的所有购物车项目然后加载它们呢?提前非常感谢。
【问题讨论】:
标签: ios swift google-cloud-firestore queue semaphore