8

This is a copy of a thread I've created on Apple's Forum here

Background

I have an established app in the App Store which has been using NSPersistentCloudKitContainer since iOS 13 without any issues. I've been running my app normally on an iOS device running the iOS 15 betas, mainly to see problems arise before my users see them. Ever since iOS 15 beta 4 my app has failed to sync changes - no matter how small the change. An upload 'starts' but never completes. After a minute or so the app quits to the Home Screen and no useful information can be gleaned from crash reports. Until now I've had no idea what's going on.

Possible Bug in the API?

I've managed to replicate this behaviour on the simulator and on another device when building my app with Xcode 13 (beta 5) on iOS 15 (beta 5). It appears that NSPersistentCloudKitContainer has a memory leak and keeps ramping up the RAM consumption (and CPU at 100%) until the operating system kills the app. No code of mine is running. I'm not really an expert on these things and I tried to use Instruments to see if that would show me anything. It appears to be related to NSCloudkitMirroringDelegate getting 'stuck' somehow but I have no idea what to do with this information. My Core Data database is not tiny, but not massive by any means and NSPersistentCloudKitContainer has had no problems syncing to iCloud prior to iOS 15 (beta 4). If I restore my App Data (from an external backup file - 700MB with lots of many-many, many-one relationships, ckAssets, etc.) the data all gets added to Core Data without an issue at all. The console log (see below) then shows that a sync is created, scheduled & then started... but no data is uploaded. At this point the memory consumption starts and all I see is 'backgroundTask' warnings appear (only related to CloudKit) with no code of mine running.

CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExporter analyzeHistoryInStore:withManagedObjectContext:error:](501): <PFCloudKitExporter: 0x600000301450>: Exporting changes since (0): <NSPersistentHistoryToken - {
    "4B90A437-3D96-4AC9-A27A-E0F633CE5D9D" = 906;
}>

CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExportContext processAnalyzedHistoryInStore:inManagedObjectContext:error:]_block_invoke_3(251): Finished processing analyzed history with 29501 metadata objects to create, 0 deleted rows without metadata.

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:](2800): <NSCloudKitMirroringDelegate: 0x6000015515c0> - Beginning automated export - ExportActivity:
<CKSchedulerActivity: 0x60000032c500; containerID=<CKContainerID: 0x600002ed3240; containerIdentifier=iCloud.com.nitramluap.Somnus, containerEnvironment="Sandbox">, identifier=com.apple.coredata.cloudkit.activity.export.4B90A437-3D96-4AC9-A27A-E0F633CE5D9D, priority=2, xpcActivityCriteriaOverrides={ Priority=Utility }>

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate executeMirroringRequest:error:](765): <NSCloudKitMirroringDelegate: 0x6000015515c0>: Asked to execute request: <NSCloudKitMirroringExportRequest: 0x600002ed2a30> CBE1852D-7793-46B6-8314-A681D2038B38

2021-08-13 08:41:01.518422+1000 Somnus[11058:671570] [BackgroundTask] Background Task 68 ("CoreData: CloudKit Export"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

2021-08-13 08:41:03.519455+1000 Somnus[11058:671570] [BackgroundTask] Background Task 154 ("CoreData: CloudKit Scheduling"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

Just wondering if anyone else is having a similar issue? It never had a problem syncing an initial database restore prior to iOS 15 (beta 4) and the problems started right after installing iOS 15 (beta 4). I've submitted this to Apple Feedback and am awaiting a response (FB9412346). If this is unfixable I'm in real trouble (and my users are going to be livid). Thanks in advance!

EDIT: It appears to be resolved from iOS 15.2 (beta 2) but the official word form Apple is that it is ‘unresolved’. I suspect this is because it’s still in testing and it may well be marked as resolved when 15.2 is made public

Paul Martin
  • 699
  • 4
  • 13
  • 1
    From user 'talking small' on the Apple Developer Forums, it appears this is failing with *relationships* being synced (or not) to iCloud. This is a HUGE problem as this was the entire point of Apple introducing NSPersistentCloudKitContainer - to get around the relationship count limit (750 for CKReferences) AND to allow many-to-may relationships to sync with CloudKit. Apple has broken something in iOS 15 beta 4 and I sure hope they fix it before release as this is going to be a nasty bug otherwise... – Paul Martin Sep 01 '21 at 06:57
  • I have only one entity in my app with no relations. Still iOS 15 devices are out of sync from the first change of data on them (no crash in my case, but out of sync is even worse - users will not know, that their data is not on iCloud). Maybe there is a problem with set of particular attributes (I have String, Int32, Bool, UUID, Binary Data), but I can't test it separately. – mosariot Sep 01 '21 at 07:23
  • @mosariot - it's definitely a problem within the CloudKit API somewhere and was introduced in iOS15 beta 4 - my data model was never modified and the failures started occurring the moment beta 4 was installed. I've been using NSPersistentCloudKitContainer in my app (in the App Store) for over 18 months and have made no changes to that part of my app's codebase. Mine crashes because the NSCloudKitMirroringDelegate gobbles up all resources trying to sync relationships. – Paul Martin Sep 02 '21 at 07:10
  • 1
    Still not working as of iOS 15 beta 8 and Xcode beta 5 sadly. Fingers crossed it will be fixed in the next update, presumably tomorrow when they announce new devices. If this isn’t fixed before release, it’s going to be a real concern. – Paul Martin Sep 14 '21 at 01:18
  • 2
    Still broken in the *release candidate* of iOS15 & Xcode 13. I suspect this is going to catch other developers by surprise very soon, as their users complain of sync failures & crashes – Paul Martin Sep 14 '21 at 21:54
  • 2
    I'm liaising with an Apple Engineer directly now so when this is resolved (either it's an Apple bug or my bug, only revealed after Apple made some change) I'll report back, either way. – Paul Martin Sep 30 '21 at 07:30
  • Update: Seems to be a bug with NSCloudKitMirroringDelegate when a certain threshold of Core Data objects (particularly relationships) are being synced for a single change - and it seems to be a Framework bug. Apple is working on a fix. The discussions I’ve been having with their DTS Engineer have been very productive and professional. Will update when I have more. – Paul Martin Oct 10 '21 at 22:19
  • I should also add, that despite not having updated my app in the App Store for 4 months, my AppStoreConnect data shows crashes rising dramatically as users start running it in iOS 15. This helps confirm that it’s an inherent bug in iOS 15 which is good to know. – Paul Martin Oct 10 '21 at 22:22
  • 1
    I'm seeing record relations no longer syncing and crashes in iOS15 as well as negative reviews appearing, all seems to be related to this bug, I'm surprised more people are not hitting this? – Theo Lampert Oct 11 '21 at 09:21
  • @theolll Yes, I'm surprised too... but I feel they will be made aware of this very soon by their users! I put a little 'Warning iOS 15 Users' note on my website and while I've seen lots of crashes, I've had no negative feedback... yet. I just hope when it's fixed that any pending changes that haven't synced due to this bug will sync when it's fixed so the users' data will be back to normal. It's going to be messy if it will require a 'reset' of their synced data! – Paul Martin Oct 12 '21 at 10:21
  • Any Updates on this one? I am experiencing the same issue with my App. – Sebastian Boldt Oct 24 '21 at 20:20
  • Not yet. All I have is that Apple is aware there is a bug/issue and they’re working on it. As soon as I hear anything back I’ll post here immediately. I hope it’s soon because I’ve stopped my development until it’s resolved. – Paul Martin Oct 25 '21 at 22:13
  • No formal updates yet. Just checking in to post this. It seems other developers are starting to notice this bug. – Paul Martin Nov 08 '21 at 02:18
  • OK… so it appears to be resolved from iOS 15.2 (beta 2) but the official word form Apple is that it is ‘unresolved’. I suspect this is because it’s still in testing and it may well be marked as resolved when 15.2 is made public - fingers crossed!! – Paul Martin Nov 18 '21 at 20:15
  • Still haven't had an official reply from Apple despite iOS15.2 being in the wild for some time now. I'll reach out to them via my TPS for confirmation, but it certainly appears to all be working. – Paul Martin Feb 06 '22 at 03:33
  • Just a final update. Since 15.5 the NSPersistentCloudKitContainer syncing has been fast, and complete... and seems to now properly sync in the background without causing crashes. FINALLY!! (just before iO16 is announced! ) – Paul Martin May 31 '22 at 06:09
  • showing up again in ios16b1 – ngb Jun 19 '22 at 02:01
  • Really? I'm not seeing any issues with data syncing in my App Store version of my App running on iOS & macOS betas (currently beta 1). – Paul Martin Jun 20 '22 at 08:42
  • @Paul Martin test in iOS 16.4.1 , but BUG still exists... – hopy Apr 28 '23 at 11:53

1 Answers1

0

After trying, the best solution currently is:

  1. Place the Data attribute in the managed object as a separate relationship in the relationship;
  2. Save the managed object itself first, delay for a few seconds, and then save its relationship

But sometimes it can still cause memory explosion and instability!

hopy
  • 559
  • 3
  • 18