1

My watch extension fetches many items from coreData in a background thread, using this code (shortened):

    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
            }
        }
    }

This code crashes with the following log:

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  

Obviously, it takes too long.

My questions:

  1. Is there any time limit for a coreData background thread?
  2. If so, how could I modify my code to avoid it?
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116

0 Answers0