0
votes

I am currently porting my data from Parse to CloudKit. I was able to import my data into the CloudKit structure, but now I am not receiving the correct amount of records. Instead of the 52 users (which are shown in the dashboard) I receive 289 records. I tried it with simple CKQuery and CKQueryOperation.

CKQuery returns 100 results due the limit and when I am using the CKQueryOperation i receive 289 results with many duplicate entries.

I use the following code (CKQuery way..) to fetch all my users:

let query = CKQuery(recordType: "User", predicate: NSPredicate(value:true))

let container = CKContainer.defaultContainer()
let publicDB = container.publicCloudDatabase

publicDB.performQuery(query, inZoneWithID: nil) { (records, error) in
    print("USER public count: \(records?.count)")
}

And for the CKQueryOperation I have written a helper Library which is porting the result automatically into a swift class structure.

let uw = ROCloudBaseWebservice<User>()

uw.load { (data) in
    logger.debug("User count (CKQueryOperation): \(data.count)")
}

If you want to see the code from this load you can see it here: https://github.com/prine/ROCloudModel/blob/master/Source/ROCloudBaseWebservice.swift

I have the same with my 3 other tables where I also get the wrong amount of data. Only one table which has 4 entries is receiving the correct amount of data.

Did anybody else have this problem? I am using References in the tables. Can this lead to a problem?

2

2 Answers

0
votes

I just found out, that there is a bug in CloudKit, that deleted records are somehow hidden still available. I resetted my development environment and imported the data again and I got the correct amount of records returned.

More information can be found in this Stackoverflow post: Deleted CloudKit records Reappear

0
votes
    func loadAllClients() {
        let pred = NSPredicate(value: true)            
        let sort = NSSortDescriptor(key: "Nome", ascending: true)
        let query = CKQuery(recordType: "Cliente", predicate: pred)
        query.sortDescriptors = [sort]

        let operation = CKQueryOperation(query: query)
        operation.desiredKeys = ["Nome", "Endereco","Telefone","Email","Observacao","Quartos","Banheiros","Frequencia","ValorPadraoST","ValorPadraoDC","ValorPadraoMC","Marketing","MensagemData","QtdAtendimento","ultimoAtendimento","DeuReview"]
        operation.resultsLimit = 400

        var newWhistles = CLIENTES

        operation.recordFetchedBlock = { record in
            newWhistles.append(record)
        }


        operation.queryCompletionBlock = { [unowned self] (cursor, error) in
            DispatchQueue.main.async {
                if error == nil {
                    //ViewController.isDirty = false
                    self.clienteRecords = newWhistles
                    //self.tableView.reloadData()


                    print("clienteRecords \(self.clienteRecords.count)")

                } else {
                    let ac = UIAlertController(title: "Fetch failed", message: "There was a problem fetching the list of CLIENTES; please try again: \(error!.localizedDescription)", preferredStyle: .alert)
                    ac.addAction(UIAlertAction(title: "OK", style: .default))
                    self.present(ac, animated: true)
                }
            }
        }

        database.add(operation)
    }