简体   繁体   English

为什么 Apple Watch 上的 CloudKit 超级慢?

[英]Why is CloudKit on Apple Watch super slow?

I am using CloudKit to store very simple workout data.我正在使用 CloudKit 来存储非常简单的锻炼数据。 The quantity is negligible.数量可以忽略不计。

I am using the same code to interact with CloudKit for the iOS app as well as the watchOS app.对于 iOS 应用程序和 watchOS 应用程序,我使用相同的代码与 CloudKit 进行交互。 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.在 iOS 上加载几乎是即时的,在 watchOS 上这可能需要几分钟,这基本上是不可用的。 Sporadically the loading speed on watchOS can be decent. watchOS 上的加载速度偶尔会不错。

What could be the cause?可能是什么原因?

Concept概念

qualityOfService is set to default when you don't assign a configuration.当您不分配配置时, qualityOfService设置为默认值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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