I need help with quering cloudkit

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

}

@nixxe Do you still have issues with this?

Hi,
actually, I found a work around.
The problem is I need to query on ‘Rang’ AND on ‘Number’. That is not possible in one go if you have several Numbers.
So here is the work around : I put the Numbers in an array and query for all the Numbers in the array.
This gives me too many results, but now I select only the correct Rang from the results.
Having 5 different Rangs in my data model, it gives me 5 times more results. But it is 10 times faster than doing 4 successive queries.

nixxe

@nixxe Thank you for sharing your solution - much appreciated! :]

This topic was automatically closed after 166 days. New replies are no longer allowed.