Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过循环SWIFT将数据删除到Firestore数组字段值的延迟_Ios_Swift_Google Cloud Firestore_Queue_Semaphore - Fatal编程技术网

Ios 通过循环SWIFT将数据删除到Firestore数组字段值的延迟

Ios 通过循环SWIFT将数据删除到Firestore数组字段值的延迟,ios,swift,google-cloud-firestore,queue,semaphore,Ios,Swift,Google Cloud Firestore,Queue,Semaphore,我正试图通过如下的for循环删除所有购物车项目: func removeRelatedProductFromUserCart(selectArrayNodeIds: [String], completion: @escaping(Error?)->()) { let semaphore = DispatchSemaphore(value: 0) let serialQueue = DispatchQueue(label: "com.queue.Serial"

我正试图通过如下的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上的所有购物车项目后再加载它们呢?提前非常感谢。

1-Firebase在后台线程中运行,因此不需要

let serialQueue = DispatchQueue(label: "com.queue.Serial")
2-您需要一个
DispatchGroup

func removeRelatedProductFromUserCart(selectArrayNodeIds: [String], completion: @escaping(Bool)->()) {

           let g = DispatchGroup() 
            for nodeId in selectArrayNodeIds {
                 g.enter()
                 FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList: false, item: nodeId) { (error) in  
                   q.leave()
                } 
            } 
         g.notify(queue:.main) {
            completion(true)
         }
}

1-Firebase在后台线程中运行,因此不需要

let serialQueue = DispatchQueue(label: "com.queue.Serial")
2-您需要一个
DispatchGroup

func removeRelatedProductFromUserCart(selectArrayNodeIds: [String], completion: @escaping(Bool)->()) {

           let g = DispatchGroup() 
            for nodeId in selectArrayNodeIds {
                 g.enter()
                 FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList: false, item: nodeId) { (error) in  
                   q.leave()
                } 
            } 
         g.notify(queue:.main) {
            completion(true)
         }
}

非常感谢你的回答。最后一点是如何在
notify(queue:main)
中发送
error
?在这种情况下,我甚至应该处理删除项的每个错误吗?不,你不应该,也应该在
updateData
的成功中添加布尔补全,尽管我像这样添加
var err:error?对于SelectArrayNodeId{dispatchGroup.enter()FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList:false,item:nodeId){(错误)in if error!=nil{error=error}dispatchGroup.leave()}dispatchGroup.notify(队列:.main){completion(err)}
,但我肯定会忽略它。再次感谢。祝你今天愉快。非常感谢你的回答。最后一点是如何在
notify(queue:main)
中发送
error
?在这种情况下,我甚至应该处理删除项的每个错误吗?不,你不应该,也应该在
updateData
的成功中添加布尔补全,尽管我像这样添加
var err:error?对于SelectArrayNodeId{dispatchGroup.enter()FirebaseManager.shared.removeProductFromUsersWishOrCartList(isForWishList:false,item:nodeId){(错误)in if error!=nil{error=error}dispatchGroup.leave()}dispatchGroup.notify(队列:.main){completion(err)}
,但我肯定会忽略它。再次感谢。祝您有个美好的一天。