Home > Software design >  Why is CloudKit on Apple Watch super slow?
Why is CloudKit on Apple Watch super slow?

Time:08-14

I am using CloudKit to store very simple workout data. The quantity is negligible.

I am using the same code to interact with CloudKit for the iOS app as well as the watchOS app. This is the code I use for loading data:

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: recordType.rawValue, predicate: predicate)
let queryOperation = CKQueryOperation(query: query)

var results: [CKRecord] = []

queryOperation.recordFetchedBlock = { (record: CKRecord ) in
  results.append(record)
}

queryOperation.queryCompletionBlock = { (cursor: CKQueryOperation.Cursor?, error: Error?) in
  if let error = error {
    print("Getting all "   recordType.rawValue   " records with CloudKit was unsuccessful", error)
    response(false, nil)
    return
  }
  if cursor == nil {
    response(true, results)
    return
  }
  let nextOperation = CKQueryOperation(cursor: cursor!)
  nextOperation.recordFetchedBlock = queryOperation.recordFetchedBlock
  nextOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
  privateDatabase.add(nextOperation)
}

privateDatabase.add(queryOperation)

On iOS the loading is almost instant, on watchOS this can take minutes which is basically unusable. Sporadically the loading speed on watchOS can be decent.

What could be the cause?

CodePudding user response:

Concept

qualityOfService is set to default when you don't assign a configuration.

Assume the watch is low on battery then system decides whether to process the operation immediately or later.

So setting it explicitly might help the system determine how you would like the operation to be handled.

Code

Can you try setting the configuration as follows:

let configuration = CKOperation.Configuration()
configuration.qualityOfService = .userInitiated
queryOperation.configuration = configuration

queryOperation.queuePriority = .veryHigh //Use this wisely, marking everything as very high priority doesn't help
  • Related