6

Core Data model attribute with type binary and "Allow External Storage" enabled cause crash in NSPersistentCloudKitContainer sync mechanism.

This happens only on receiving device, meaning upload to CloudKit is successful, but download from CloudKit causes crash when in NSManagedContext.

<NSSQLSaveChangesRequestContext: 0x281ec8420> , *** NSAllocateMemoryPages(28317598) failed with userInfo of (null)

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(30318498) failed

This is obviously a memory crash, but I can't figure out how to workaround it, it seems to be an iOS bug.

Alexandr Mazanov
  • 443
  • 1
  • 5
  • 6
  • My guess is that it is not a bug and you're just overloading the system. Read this, it may help... https://blog.five.agency/how-to-import-a-large-data-set-using-core-data-6c248a503148 – andrewbuilder Oct 21 '19 at 09:11
  • ...and the [Apple Docs](https://developer.apple.com/documentation/foundation/1416210-nsallocatememorypages?language=objc) indicate that the process to allocate a new block of memory has failed. – andrewbuilder Oct 21 '19 at 09:23
  • @andrewbuilder I understand that the crash is caused during memory allocation, more than that I know that it tries to allocate memory specifically for the binary attribute(exact size byte to byte). What I don't understand is why it's doing it? My expectations of how it should work: 1) I create a binary attribute and allow external storage, i.e. for large values core data stores a file + reference to file 2) CloudKit Stores is as CKAsset 3) At no point during synchronization, this data should be lifted into the memory... – Alexandr Mazanov Oct 22 '19 at 13:27

1 Answers1

6

You are not alone! It's not something you are doing wrong, if you disable "Allow External Storage" option (and delete the app from your device to avoid migration crash) it will sync fine. Obviously it's something wrong with "Allow External Storage" option, and files bigger than a few mb.

In my app I was syncing png images. Those who were bigger than ~28mb (if I can recall the size correctly), couldn't sync to a device and made it crash.

I submitted a bug report with Feedback Assistant, and for the first time Apple asked for more details. I send them a sample app.

Edit: Good news folks! Apple flagged my bug report with "Potential fix identified - For a future OS update". I'll check Xcode 12 beta if its fixed and report back.

Edit 2: For iOS 14 and Xcode 12, the bug has been fixed! Yay!

  • I've reported it to Apple too, but never heard back :) In my case, as a work around, I've disabled "Allow External Storage", and surprisingly(!) it works fine. I don't like having bloated data base, but hey, at least it doesn't crash. – Alexandr Mazanov May 17 '20 at 18:31
  • Also ran into this crash with a 26 mb pdf. Disabling "Allow External Storage" fixed it but I wonder what performance penalty that has. – Bret Jun 25 '20 at 21:18
  • Hey Nikos, how it going now? Did Apple fix this problem in Xcode 12? – francisfeng Aug 27 '20 at 06:49
  • @francisfeng for Xcode 12 beta 6, unfortunately it's not fixed. I haven't tried it though with iOS 14 beta on a real device. – Nikos Polychronakis Aug 27 '20 at 20:53
  • I thought it were fixed in iOS 14. But I run into the same problem once the file reached 100MB. Just frustrated. – francisfeng Mar 31 '21 at 15:16
  • I *did* try it with iOS 14, after Apple responded that it fixed it, and it worked fine. Maybe an update broke it again. Feedback Assistant my old friend... – Nikos Polychronakis Mar 31 '21 at 17:14