How can i fix the error on the DispatchGroup leave in swift

I want to load all data from firebase, then show the data to the table view. But now, I can't show all the data to the table view. It is because call the finishLoading(realm) method is faster than the for loop get all the data. How can I do some show all data when for loop is finish in swift. I have to use the Closure, however the second of the loop is later than this "self.finishLoading(realm: realm)"

I have to try to add the DispatchGroup(), however, the leave() when having an error of EXC_BAD_INSTRUCTION. Can I put the leave() in the closure? How can I fix it?

  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)
         }
    }