Я пытаюсь загрузить данные из Firestore, добавить их в массив, а затем добавить в другой массив, как только все данные будут загружены. Я пробовал использовать обработчик завершения, но он возвращается, даже если не все данные были загружены. Итак, я попытался использовать DispatchGroup, однако dispatch.notify вызывается до завершения моей задачи.
вот мой код. Я вызываю dispatch.begin внутри последней скобки else, иначе по какой-то причине left и enter не сбалансированы?
Как мне подождать, пока все данные будут загружены, а затем вызвать завершение внутри dispatch.notify ?
func SameUniRelatedCourses(completion: @escaping (_ success: Bool) -> Void) {
DispatchQueue.main.async {
self.spinner.startAnimating()
}
service.loadUniversityAndCourse { (uni, course) in
let related = RelatedCourses()
let relatedCourseArray = related.getRelatedCourses(userCourse: course)//.prefix(4)
for Course in relatedCourseArray {
let UniRef = Firestore.firestore().collection("User-Courses").document(Course)
UniRef.getDocument { (snapshot, error) in
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 {
let usersRef = Firestore.firestore().collection("users").document(user)
usersRef.getDocument { (snapshot, error) in
if let error = error {
print(error.localizedDescription)
}
else {
let data = snapshot?.data()
//print("raw data", data?["username"]!)
if let dictionary = data as [String:AnyObject]? {
let Info = UserInfo(dictionary: dictionary)
if Info.University != uni {
//print("Not in their uni",Info.username!)
}
else if self.sameUniSameCourse.contains(where: { $0.uid == Info.uid }) {
//print("already in sameunisamecourse",Info.username!)
}
else {
print("entering")
self.dispatchGroup.enter()
print("1", Info.username!)
self.sameUniRelatedCourses.append(Info)
self.sameUniRelatedCourses.sort { (time1, time2) -> Bool in
return time2.Created!.seconds/1000 > time1.Created!.seconds/1000
}
print("leaving")
self.dispatchGroup.leave()
}
}
}
}
}
}
}
}
}
self.dispatchGroup.notify(queue: .main) {
print("done")
}
}
вторая функция
func sameUniversity(completion: @escaping (_ success: Bool) -> Void) {
DispatchQueue.main.async {
self.spinner.startAnimating()
}
self.dispatchGroup.enter()
service.loadUniversityAndCourse { (uni, course) in
defer{ self.dispatchGroup.leave() }
let UniRef = Firestore.firestore().collection("User-Universities").document(uni)
UniRef.getDocument { (snapshot, error) in
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 {
let usersRef = Firestore.firestore().collection("users").document(user)
usersRef.getDocument { (snapshot, error) in
if let error = error {
print(error.localizedDescription)
}
else {
let data = snapshot?.data()
//print("raw data", data?["username"])
if let dictionary = data as [String:AnyObject]? {
let Info = UserInfo(dictionary: dictionary)
if self.sameUniSameCourse.contains(where: { $0.uid == Info.uid }) || Info.uid == Auth.auth().currentUser?.uid || self.sameUniRelatedCourses.contains(where: { $0.uid == Info.uid }) {
//print("already in master array", Info.username!)
}
else {
print("2", Info.username!)
self.sameUni.append(Info)
self.sameUni.sort { (time1, time2) -> Bool in
return time1.Created!.seconds/1000 > time2.Created!.seconds/1000
}
}
}
}
}
}
}
}
}
self.dispatchGroup.notify(queue: .main) {
print("done")
//call Completoion here
self.masterArray.append(contentsOf: self.sameUni)
self.spinner.stopAnimating()
self.tableView.reloadData()
completion(true)
}
}