0

Hi I am trying to make a small function that should send me back the record that corresponds to the user by entering his/her name. I created a swift file:

import Foundation
import CloudKit
import UIKit


func getGetMainUser(name : String) -> CKRecord {
var myRecord : CKRecord!
let container = CKContainer.defaultContainer()
let publicDB = container.publicCloudDatabase
let predicate = NSPredicate(format: "nickName == name")
let query = CKQuery(recordType: "usersAtributes", predicate: predicate)

publicDB.performQuery(query, inZoneWithID: nil) {
    record, error in {
        if error != nil {
            println(error.localizedDescription)
        } else {
            if record.count == 1 {
                myRecord = record[0] as CKRecord
            }
        }
    }
}
return myRecord
}

What I don't understand is that I cannot compile this code because of the nil value of inZoneWithID of the performQuery. This syntax might be incorrect though it works in other parts of my code. This is the error I get :

Cannot convert the expression's type '(CKQuery!, inZoneWithID: NilLiteralConvertible, ((ST5,(ST5,ST6) -> (ST5, ST6) -> ST4) -> (ST5,(ST5,ST6)-> ST4) -> ST4,((ST5,ST6) -> (ST5,ST6)...
Quentin Malgaud
  • 405
  • 6
  • 21

2 Answers2

0

Your problem is not the zoneID. The documentation for that says this:

The ID of the zone to search. Search results are limited to records in the specified zone. Specify nil to search the default zone of the database.

It's the predicate that's wrong. It should be:

NSPredicate(format: "nickName = %@", name)

And one other thing, the performQuery will be executed asynchronous. So you are returning myRecord while it has not been set yet. You have 2 options.

  1. You could use semaphores to make this method synchronous.
  2. Instead of returning a value you could call the method while passing on a code block. Then call that code block with that record.

Option 2 is the preferred method. Option 1 will lock your app during the CloudKit call.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • Hi Edwin, nickName is valid in the recordType. And name is an argument of the function: if I call getGetMainUser(Ralf), the function will fetch in usersAtributes and return the record that has Ralf as a nickName. But the problem really comes from inZoneWithID: nil. Because the "nil" is in red and if I comment all the performQuery block, the code does compile. Is this a bug from Xcode? – Quentin Malgaud Feb 17 '15 at 16:25
  • Ah, now I see it... You have to pass on the name variable as an argument. I just updated the answer with the correct predicate. – Edwin Vermeer Feb 17 '15 at 18:38
  • Thank you I knew it was a bit approximative. This solution looks really clear and should be working. However, it is not. It is getting really annoying. It still points out that "nil" is the problem. I'm trying to find someone else's computer and run the code to see what happens. Because this makes no sense to me. – Quentin Malgaud Feb 17 '15 at 19:06
  • Another ah... :-) You cannot return the record. I updated the answer again – Edwin Vermeer Feb 17 '15 at 19:54
0

okay, I finally found my (newbie) mistake: I added two extra/useless brackets that where causing an error. Opening right after

record, error in

and closing right before

return myRecord 

Now that I've deleted them, everything is fine. Thank you Edwin, your useful answer will help me a lot now that the code compiles.

Quentin Malgaud
  • 405
  • 6
  • 21