I am trying to write a thumbnail provider extension (on macOS) that accesses the Core Data store of the main application, shared via an app group, to find images to base icon thumbnails on. This worked fine until I added one attribute to the Core Data model. Now the extension always crashes with An error occurred during persistent store migration
and attempt to write a readonly database
.
Creating a new default model version and deleting the Core Data Store doesn't help. Somehow the thumbnailer extension still thinks that the store written by the main application uses an earlier model, which makes no sense.
If I set shouldMigrateStoreAutomatically
to NO, I get The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store.
If I set readOnly = YES
on the NSPersistentStoreDescription, I get The file couldn’t be saved because you don’t have permission.
which I think may be a concurrency isse, caused by lots of thumbnail provider threads trying to migrate the store all at once.
EDIT: Original code here: https://github.com/angstsmurf/spatterlight/blob/quicklook/SpatterlightThumbnails/ThumbnailProvider.m
Right, I got this to work, but I'm not sure how.
Some of the things I did: cleaned up the build folder, deleted all copies of the main application from my hdd, built a new release version of it, put it in Applications, picked that one when Xcode asked me to choose an app to run after building the thumbnail provider extension. That still didn't work, but afterwards I no longer got any store migration errors when building and running in Xcode.
So basically the problem seems to have been that Xcode picked an old build of the main application to run along with the extension, which created an old version of the Core Data store. Or the old store was still being cached somewhere. Just cleaning the build folder was not enough to fix this.