15

I am trying to use CKQueryOperation, rather then performQuery on my CloudKit database.

Both work, but when using a CKQueryOperation I am not getting an error when the device is offline, but I do when using performQuery

Here is the bare bones my performQuery example, database is my CKDatabase

database.performQuery(q, inZoneWithID: nil) { (records:[CKRecord]?, error:NSError?) in
    if error != nil {
        print(error!.localizedDescription)
        return
    }
}

An error is given when the device is offline, allowing me to prompt the user. The error is

The internet connection appears to be offline

However, I get no errors when I use a CKQueryOperation

let p = NSPredicate(format:"recordID IN %@", student.courses)
let q = CKQuery(recordType: String(Course), predicate: p)

let queryOperation = CKQueryOperation(query: q)

queryOperation.recordFetchedBlock = { record in
    // not called without network connection - doesn't enter scope
    print(record)
}


queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) in
    // not called without network connection - doesn't enter scope
    print(cursor)
    print(error)
}

database.addOperation(queryOperation)

With a connection I receive my data for both methods so it is working as expected.

How / Where am I informed of error when using CKQueryOperation ?

Thanks

DogCoffee
  • 19,820
  • 10
  • 87
  • 120

1 Answers1

23

As per usual I post a bounty and find the answer within the next hour or 2. Not sure how I missed this originally but it contained the answer I was looking for.

So by adding this line

queryOperation.qualityOfService = .UserInitiated

something behind the scenes changes and we have some nice action within

queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) in
    // We get an error message... Finally !!
    print(error)
}

Couldn't find anything in the Apple Docs to hint at this either.

Community
  • 1
  • 1
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
  • 1
    I also found that the operations `timeoutIntervalForResource` default value is 7 days... So it might take a week before giving up in the request, even if it sounds silly. I changed it to 6 seconds and voilá – Bernardo Santana Feb 01 '17 at 14:57
  • It looks like the timeout intervals are [deprecated](https://developer.apple.com/documentation/cloudkit/ckoperation), but thankfully qualityOfSetting still works. It is now `.userInitiated` in Swift 3. – James Toomey Aug 08 '18 at 05:24
  • My .queryCompletionBlock wasn't being called at all until I added in the .qualityOfService. Thank you! – Kevin Feb 13 '21 at 05:20