Core Data + CloudKit integration, large binary files sync crash
Asked Answered
D

1

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.

Dibasic answered 19/10, 2019 at 14:28 Comment(3)
My guess is that it is not a bug and you're just overloading the system. Read this, it may help... blog.five.agency/… – Soane
...and the Apple Docs indicate that the process to allocate a new block of memory has failed. – Soane
@Soane 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... – Dibasic
M
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! πŸŽ‰

Mismate answered 16/5, 2020 at 20:50 Comment(6)
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. – Dibasic
Also ran into this crash with a 26 mb pdf. Disabling "Allow External Storage" fixed it but I wonder what performance penalty that has. – Sheikdom
Hey Nikos, how it going now? Did Apple fix this problem in Xcode 12? – Stempien
@Stempien for Xcode 12 beta 6, unfortunately it's not fixed. I haven't tried it though with iOS 14 beta on a real device. – Mismate
I thought it were fixed in iOS 14. But I run into the same problem once the file reached 100MB. Just frustrated. – Stempien
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... – Mismate

© 2022 - 2024 β€” McMap. All rights reserved.