App getting killed by iOS while NSPersistentCloudKitContainer syncs

I have been spending the last several weeks implementing NSPersistentCloudKitContainer in my app, and it is most of the way there. Unfortunately, I keep running into an issue where after several days of successful syncing between devices, each device begins to crash after about a minute of use, repeatedly.

The crash report points to a SQL thread doing things with the CoreData and CloudKit frameworks — none of my code whatsoever. It is the typical “CPU:  48 seconds cpu time over 58 seconds (82% cpu average), exceeding limit of 80% cpu over 60 seconds” issue. If I run the devices hooked up to Xcode and debug, I see the thread spin up and the log shows it chugging through changed CKRecords it needs to import, just like normal. If I leave the devices hooked up to Xcode, they eventually make it through this huge job and the devices become usable again.

Once one device is in this state, the problem also occurs on new devices trying to download from the cloud for the first time.

I’ve attached a screenshot of the stacktrace of that thread in Instruments. I haven’t had any luck finding other people mentioning the system killing their app during a sync, so I’m kind of at a loss for what to do. It seems like the issue is occurring in a job that the NSPersistentCloudKitContainer is managing on my behalf and I haven’t been able to figure out a way to configure a timeout or anything.  

Has anyone experienced this? I’m not sure what to do if the chunks that NSPersistentCloudKitContainer breaks up the import into are too large for the device to work through before the system kills the app. I’d appreciate any ideas or insights. Please let me know if any other information would be helpful.

Thanks!

I still have not figured out what is causing this. Am I really the only person getting cpu_usage_fatal crashes caused by CoreData/CloudKit syncs?

I'm facing a the same problem.

In my case I'm collecting data in background mode. If internet connection gets lost, large amount of data might be sync when the device gets back on the internet. If data collection is still active, the background mode seems to keep NSPersistentCloudKitContainer alive resulting in the iCloud sync not being terminated and the app getting killed due to high CPU pressure.

What framework are you using? I'm using SwiftUI. I'm also still looking for a solution.

OK, commenting on your thread as I have the same issue (which you've found). Let's hope Apple is able to track down what's going on here. Only started with iOS15 beta 4 from my testing. https://developer.apple.com/forums/thread/687877

This seemed to coincide with other changes to NSPersistenCloudkitContainer's console outputs when syncing since beta 4. Something big has been changed behind the scenes I think and it's very much 'beta' again.

That call tree image is practically identical to mine. In a way I'm relieved as it's unlikely to be my fault! 😅

Have you received any update from Apple on this? They have not replied to my Feedback report at all - not even acknowledged the receipt of my test data, or if it might even be something I’ve done wrong. Nothing.

We’re about a month away from release and this is going to break my app if not fixed - which has been working fine for the past 2 years using NSPersistentCloudKitContainer. I suspect there will be a lot of developers & users that will be caught off-guard if it’s released to the public as it’s only a ‘visible’ problem for those of us with slightly complex Core Data models.

My app is a medical record keeping app and my users are going to be extremely angry when it fails to sync after they upgrade to iOS15 if this isn’t fixed.

In my line of work (I’m an MD) this poor (or no) communication would see me sued by my patients (and quite rightly too) - people just need to know something is being done.

Something fundamentally changed with NSPersistentCloudKitContainer from beta 4 and this was obvious when you looked at the formatting of the console output when a sync did occur. It was totally different suddenly - it was the first thing I noticed. The second thing I noticed was that it didn’t work…

This is the first time, since I returned to Apple in 2004, that I feel really disappointed in them. Sigh.

Please file a feedback report and include your instruments trace file. A link to this thread would also be helpful.

These aren't all the same issue. FB9489988 here is due to additional restrictions being applied by adopting the background music playing entitlement. This should be fixed in the next iOS 15.1 beta, which I think will be beta 3.

Same here. The problem is from iOS 15 beta 4 to now(iOS 16 beta 6). Sync in iOS 14 is so nice.

App getting killed by iOS while NSPersistentCloudKitContainer syncs
 
 
Q