I am running four queries on cloudkit, one after the other. Sometimes however, I only get 3 correct records. I am doing something wrong, but I don’t see it. All help is appreciated.
// THis is the function which included the call to the different queries
func GeefEenQuizwoord (completed: @escaping (Int) → Void) {
AantallenOphalen {(aantalLijst) in
DispatchQueue.main.async {
woordenLijst = []
Vertaling1Ophalen {(woord) in
DispatchQueue.main.async {
woordenLijst.append(woord)
Vertaling2Ophalen {(woord) in
DispatchQueue.main.async {
woordenLijst.append(woord)
Vertaling3Ophalen {(woord) in
DispatchQueue.main.async {
woordenLijst.append(woord)
AantepassenWoordOphalen {(woordDB) in
DispatchQueue.main.async {
woordenLijstDB.append(aantepassenWoordDB)
//code to be executed after all words have been downloaded
gekozenWoordDB = woordenLijstDB[quizWoordKeuze]
}
}
}
}
}
}
}
}
}
}
completed(1)
}
// all queries are similar and look like this
func Vertaling1Ophalen (completionHandler: @escaping ( woord: WoordType) → Void) {
woord = WoordType(arabisch: “”, nederlands: “”, rang: -1, les: “”, nummer: 0)
let selectie = NSPredicate(format: “Rang == %@ AND Nummer == %@”, rangKeuzeInt as NSNumber, woord1Keuze as NSNumber)
let query = CKQuery(recordType: “CKWoord”, predicate: selectie)
query.sortDescriptors = [NSSortDescriptor(key: “Nederlands”, ascending: true)]
database.perform(query, inZoneWith: nil) {
results, error in
if error != nil {
print(error?.localizedDescription as Any)
} else {
for res in results! {
let woordDB = res as CKRecord
woordenLijstDB.append(woordDB)
woord.arabisch = woordDB.object(forKey: "Arabisch") as! String
woord.nederlands = woordDB.object(forKey: "Nederlands") as! String
woord.rang = woordDB.object(forKey: "Rang") as! Double
woord.les = woordDB.object(forKey: "Les") as! String
woord.nummer = woordDB.object(forKey: "Nummer") as! Double
}
completionHandler(woord)
}
}
}