簡體   English   中英

使用調度等待數據附加到數組?

[英]Using dispatch to wait for data to be appended to array?

我試圖從 Firestore 下載數據,append 將其下載到一個要排序的數組,然后在所有數據都已排序后將 append 下載到另一個數組。 我有一個調度組,它在繼續之前管理正在下載的所有數據,但是我不知道如何將排序數組(temparray1 / temparray2)append 到主數組(closeunisSameCourse/nearbyUnis)。 我嘗試使用 dispatch.wait 來等待臨時 arrays 被附加,但它只是掛起。

func nearbyUnisSameCourse(completion: @escaping (_ success: Bool) -> Void) {
        DispatchQueue.main.async {
            self.spinner.startAnimating()
        }
        self.dispatchGroup.enter()
        service.loadUniversityAndCourse { (uni, course) in
        defer{ self.dispatchGroup.leave() }
        let nearbyUnis = ClosestUnis()
        let closeUniArray = nearbyUnis.getClosestUnis(University: uni)
        for uni in closeUniArray {
            let UniRef = Firestore.firestore().collection("User-Universities").document(uni)
                self.dispatchGroup.enter()
                UniRef.getDocument { (snapshot, error) in
                defer{ self.dispatchGroup.leave() }
            if let error = error{
                print(error.localizedDescription)
            }
            else {
                //append their data to an array
                guard let data = snapshot?.data() else {return}
                let stringArray = Array(data.keys)
                for user in stringArray {
                    self.dispatchGroup.enter()
                    let usersRef = Firestore.firestore().collection("users").document(user)
                    usersRef.getDocument { (snapshot, error) in
                    defer{ self.dispatchGroup.leave() }
                if let error = error {
                    print(error.localizedDescription)
                }
                else {
                    let data = snapshot?.data()
                    if let dictionary = data as [String:AnyObject]? {
                    let Info = UserInfo(dictionary: dictionary)
                        
                        if Info.Course == course {
                           print(Info.username!)
                           self.tempArray1.append(Info)
                           self.tempArray1.sort { (time1, time2) -> Bool in
                               return Double(time1.Created!.seconds) > Double(time2.Created!.seconds)
                                }
                            self.closeunisSameCourse.append(contentsOf: self.tempArray1)
                            self.tempArray1.removeAll()
                            
                        }
                            else {
                                self.tempArray2.append(Info)
                                print(Info.username!)
                            self.tempArray2.sort { (time1, time2) -> Bool in
                                    return Double(time1.Created!.seconds) > Double(time2.Created!.seconds)
                                }
                            }
                        
                        }
                    }
                }
                    //end of for user loop
                }
                //outside user for loop
                print("now appending")
                self.nearbyUnis.append(contentsOf: self.tempArray2)
                print(self.nearbyUnis.description)
                self.tempArray2.removeAll()
                
                }}}}
        
            self.dispatchGroup.notify(queue: .main) {
            print("Finished")
            //self.spinner.stopAnimating()
            //print(self.nearbyUnis.description)
            self.tableView.reloadData()
            completion(true)
            }
        }
}

按名稱排序固定

else {
    self.nearbyUnis.append(Info)
    print(Info.username!)
    self.nearbyUnis.sort { (time1, time2) -> Bool in
    return Double(time1.Created!.seconds) > 
    Double(time2.Created!.seconds)}
    self.nearbyUnis.sort { (uni1, uni2) -> Bool in
    return uni2.University! > uni1.University!}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM