1

我的手表扩展在后台线程中从 coreData 获取许多项目,使用以下代码(缩短):

    coreDataSerialQueue.async {
        backgroundManagedContext.performAndWait {
            …
            let buyItemFetchRequest: NSFetchRequest<CDBuyItem> = CDBuyItem.fetchRequest()
            …
            do {
                let cdShoppingItems: [CDBuyItem] = try backgroundManagedContext.fetch(buyItemFetchRequest)
                …
                return
            } catch let error as NSError {
                …
                return
            }
        }
    }

此代码崩溃并显示以下日志:

Event:            cpu usage
Action taken:     Process killed
CPU:              2 seconds cpu time over 4 seconds (59% cpu average), exceeding limit of 14% cpu over 15 seconds
CPU limit:        2s
Limit duration:   15s
CPU used:         2s
CPU duration:     4s
Duration:         3.57s
Duration Sampled: 0.00s
Steps:            1  

显然,它需要的时间太长了。

我的问题:

  1. coreData 后台线程有时间限制吗?
  2. 如果是这样,我该如何修改我的代码以避免它?
4

0 回答 0