我怎样才能迅速解决DispatchGroup上的错误

时间:2019-04-21 09:40:30

标签: ios swift firebase realm

我想从firebase加载所有数据,然后将数据显示到表视图。但是现在,我无法将所有数据显示到表格视图中。这是因为调用finishLoading(realm)方法比for循环获取所有数据要快。快速完成for循环时,如何显示所有数据。我必须使用Closure,但是循环的第二个步骤比此“ self.finishLoading(realm:realm)”晚。

我必须尝试添加DispatchGroup(),但是,当出现EXC_BAD_INSTRUCTION错误时,请添加Leave()。我可以将leaves()放在关闭处吗?我该如何解决?

  func loopAllProduct(userId: String, finishLoadWhenErr:Bool, storedClosure: @escaping (DocumentSnapshot) -> Void){
        let storage = Storage.storage()
        let db = Firestore.firestore()
        let userDocRef = db.collection("Users").document(userId).collection("Product")
        userDocRef.getDocuments{(document, error) in
            if let err = error {
                print("Error getting documents: \(err)")
            } else {
                for document in document!.documents {
                    storedClosure(document)
                }
            }
        }

    }
    func downloadData() {
        let startTime = Date()
        while updating {
            let diffTime = Date(timeIntervalSinceReferenceDate: startTime.timeIntervalSinceReferenceDate)
            if (diffTime.timeIntervalSinceNow < -5){
                self.stopAnimating()
                self.refreshControl?.endRefreshing()
                print("Update Timeout")
                return
            }
        }
        updating = true
        let storage = Storage.storage()
        let db = Firestore.firestore()
        let productLoading = NSMutableArray()
        let realm = try! Realm()
        print("all posts")
        let group = DispatchGroup()
        let addPosts: (DocumentSnapshot)->Void = {(document) in
                try! realm.write {
                    if let resuls = self.realmResults {
                        realm.delete(resuls);
                    }
                }

                let product = Product()
                product.id = document.documentID
                product.userID = document.data()?["UserID"] as? String
                product.userName = document.data()?["UserName"] as? String
                product.descrition = document.data()?["Descrition"] as? String
                product.postTime = document.data()?["PostTime"] as? Date
                product.price = document.data()?["Price"] as? Double ?? 0.0
                product.stat = (document.data()?["stat"] as? Int)!
                product.productName = document.data()?["ProductName"] as? String
                let productId = document.documentID
                productLoading.add(productId)
                try! realm.write {
                    realm.add(product)
                }
             group.leave()
        }
        let userDocRef = db.collection("Users")
        userDocRef.getDocuments{(document, error) in
        for document in document!.documents {
                group.enter()
                self.loopAllProduct(userId:document.documentID , finishLoadWhenErr: true, storedClosure: addPosts)
            }
        }
        group.notify(queue: DispatchQueue.main) {
            self.finishLoading(realm: realm)
         }
    }

0 个答案:

没有答案