Próbuję pobrać dane z Firestore, dołącz go do jednej tablicy, którą należy posortować, a następnie dołączyć go do innej tablicy, gdy wszystkie dane zostały posortowane. Mam grupę przesyłową, która zarządza wszystkie dane są pobierane przed przejściem, jednak nie wiem, jak lub gdzie do dołączenia posortowanej tablicy (Temparray1 / Temparray2) do tablicy Master (CloseNissMecourse / Allunis). Próbowałem korzystać z wysyłki

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)
            }
        }
}
-1
Christian Grinling 12 lipiec 2020, 03:06

1 odpowiedź

Najlepsza odpowiedź

Naprawiono sortowanie według nazwy

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!}
}
1
Christian Grinling 12 lipiec 2020, 01:41