100% CPU, Energy Efficiency NSPersistentCloudKitContainer / Best Practices?

Hi,

I recently added iCloud sync capability to my app via NSPersistentCloudKitContainer. It does push data to the cloud, in other words seems to be working fine. However, I noticed a strong increase in battery drain and when I debug (release and debug build configs) I can see that a NSManagedObjectContext thread that is definitely not mine has 100% CPU load basically all the time. (I waited a few minutes, yes minutes, for it to finish while not using the app at all and it does not stop.)

Component utilization shows that most the energy impact is CPU and "overhead" is very low (goes to 0 quickly). There is no network activity visible, so this seems to be some kind of background processing. Also, memory is very, very low (~ 35 MB).

I am wondering if I am doing something wrong here or if there is a better practice. Some info about how my app works:

  • It downloads things from the web and generates a lot of entities while doing so.
  • It frequently saves changes (incremental changes in entities, there are lots of save() calls during a refresh).
  • I have not had PersistantHistory enabled before changing my processes to work with iCloud (existing sqlite store).

Not sure if anything else is relevant. Any help is appreciated.


A bit of log:

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:](2958): <NSCloudKitMirroringDelegate: 0x281d34680> - Beginning automated export - ExportActivity:
<CKSchedulerActivity: 0x2805bf2f0; containerID=<CKContainerID: 0x282950510; containerIdentifier=iCloud.app.***, containerEnvironment="Sandbox">, identifier=com.apple.coredata.cloudkit.activity.export.368A54A8-5EA2-4177-B6DD-5404550042AD, priority=2, xpcActivityCriteriaOverrides={ Priority=Utility }>
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate executeMirroringRequest:error:](884): <NSCloudKitMirroringDelegate: 0x281d34680>: Asked to execute request: <NSCloudKitMirroringExportRequest: 0x28337ed40> 3052F013-BA85-4BB8-8935-74F829AACA4B
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedImportWithLabel:activity:completionHandler:](2922): <NSCloudKitMirroringDelegate: 0x281d34680> - Beginning automated import - ImportActivity - in response to activity:
<CKSchedulerActivity: 0x28057a3f0; containerID=<CKContainerID: 0x282900300; containerIdentifier=iCloud.app.***, containerEnvironment="Sandbox">, identifier=com.apple.coredata.cloudkit.activity.import.368A54A8-5EA2-4177-B6DD-5404550042AD, priority=2, xpcActivityCriteriaOverrides={ Priority=Utility }>
CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate executeMirroringRequest:error:](884): <NSCloudKitMirroringDelegate: 0x281d34680>: Asked to execute request: <NSCloudKitMirroringImportRequest: 0x2833664c0> 98E2628F-6AE5-43C0-BA9E-2B72707B31DC

Profile of what that thread is doing (inverted):