简体   繁体   中英

Why is CloudKit on Apple Watch super slow?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM