# I need help with quering cloudkit

**URL:** <https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256>\
**Category:** Swift\
**Created:** [March 11, 2020, 10:24am UTC](https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256 "2020-03-11T10:24:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nixxe](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nixxe/32/81494_2.png) [@nixxe](https://forums.kodeco.com/u/nixxe)\
**Post date:** [March 11, 2020, 10:24am UTC](https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256/1 "2020-03-11T10:24:21Z")

</div>

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

```

}

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 1, 2020, 6:11am UTC](https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256/2 "2020-04-01T06:11:37Z")

</div>

@nixxe Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![nixxe](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nixxe/32/81494_2.png) [@nixxe](https://forums.kodeco.com/u/nixxe)\
**Post date:** [April 1, 2020, 7:39am UTC](https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256/3 "2020-04-01T07:39:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 1, 2020, 7:41am UTC](https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256/4 "2020-04-01T07:41:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [August 25, 2020, 2:24am UTC](https://forums.kodeco.com/t/i-need-help-with-quering-cloudkit/101256/5 "2020-08-25T02:24:21Z")

</div>

Closed on August 25, 2020, 2:24am UTC

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