NSPersistentStore migration issue

Hello,


I have an issue with store and migrations and I cannot yet figure out what it is.


I'm doing progressive manual migrations (not lightweight) and for some users, when I try to find a model compatible with the store, I'm unable to.


This seems to happen for users with a brand new store. They install the app. They open it once and either face the issue immediately, or maybe they may have closed the app quickly after launch (eg. put in the background) and reopened it later. In some cases, there might have been an instance of a NSCocoaErrorDomain 134000 error on the first launch of the app when attempting to read the metadata from a store (that didn't exist yet).


When the app opens, model.isConfiguration(withName:, compatibleWithStoreMetadata:) returns false, so I'm triggering the migration flow.


I'm iterating on a list of models to try to find the one compatible with the user's store (it should be the most recent model anyways, since the app was just installed).


None of my existing models seem to be compatible with the store. (Just to be clear, I haven't made incompatible changes to the model!)


When I inspect the metadata of the store, I'm getting the following information:


Store metadata: ["NSPersistenceFrameworkVersion": 851, "NSStoreType": SQLite, "NSStoreUUID": E968CF26-D6BB-494F-B48E-1CE0914818D2, "_NSAutoVacuumLevel": 2]


Compared to a valid store:


Store metadata: ["NSStoreUUID": 4A344B08-9C4C-4CC0-AF19-8D6717C7C691, "_NSAutoVacuumLevel": 2, "NSPersistenceFrameworkVersion": 320, "NSStoreType": SQLite, "NSStoreModelVersionHashes": {

Document = <5634b6a0 65add26b 364c9074 a55f3ea2 b102452e d4db625b 3b2c70c9 a63d2c54>;

DocumentPage = <812eeedf b5494a2a fe654167 0a6152cf 2f6879ea d29b6ac8 dc1ec6c1 2f5d5a12>;

DocumentPageImage = <e86c0059 9a03c3ff 4d80be77 b32dc201 e197b18d 13999e64 7dfc0260 3bb4209e>;

}, "NSStoreModelVersionIdentifiers": <__NSCFArray 0x604001e52150>(


)

, "NSStoreModelVersionHashesVersion": 3]


So somehow, the store seems incomplete without any information about the tables.


Last, but not least, if I try to force the persistent store coordinator to add this store, I get the following, more detailed, error:


CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/bruno/Library/Developer/CoreSimulator/Devices/39731F7D-6743-4BF4-B4A8-DA626A69475B/data/Containers/Data/Application/AFB0359F-635C-498C-8954-3646E99B63B5/tmp/Archive-incompatible-model-7A8ADDF8-50D1-4808-94FA-DEA2F97F4480.sqlite options:{

NSInferMappingModelAutomaticallyOption = 1;

NSMigratePersistentStoresAutomaticallyOption = 1;

} ... returned error Error Domain=NSCocoaErrorDomain Code=134020 "(null)" UserInfo={NSUnderlyingException=Can't find table for entity Document in database at URL file:///Users/bruno/Library/Developer/CoreSimulator/Devices/39731F7D-6743-4BF4-B4A8-DA626A69475B/data/Containers/Data/Application/AFB0359F-635C-498C-8954-3646E99B63B5/tmp/Archive-incompatible-model-7A8ADDF8-50D1-4808-94FA-DEA2F97F4480.sqlite} with userInfo dictionary {

NSUnderlyingException = "Can't find table for entity Document in database at URL file:///Users/bruno/Library/Developer/CoreSimulator/Devices/39731F7D-6743-4BF4-B4A8-DA626A69475B/data/Containers/Data/Application/AFB0359F-635C-498C-8954-3646E99B63B5/tmp/Archive-incompatible-model-7A8ADDF8-50D1-4808-94FA-DEA2F97F4480.sqlite";

}

CoreData: annotation: NSPersistentStoreCoordinator's current model hashes are {

Document = <451d1fd1 bf1fce92 1ce85eee 82623853 f9d216ed 829f12c5 8867da4d 8332d897>;

DocumentPage = <a572584a 2563ffe7 39f1accc 871e241a 6701a09a d2ca5691 450ca76b ccfd3c05>;

Export = <88e6d866 8e1ce757 bf28a339 82fcc279 1c3d9d94 6be39254 39e9e332 f3a93bcc>;

Tag = <0a096752 6f14a455 d05517d8 bbc2f36e bb0126a0 c05723b4 552efa6e eb954230>;

}


It seems as if the store wasn't created properly initially.


My question: why did this happen and how can I avoid this in the future?

For the users facing this problem: how can I detect this issue and work around it properly in the most generic way. I don't really like the idea of deleting and recreating the store if I cannot find any compatible model because it seems like I could delete the store when it's not desirable, and it looks like an ad hoc solution to me which I'd like to avoid.


Happy to open a DTS if this isn't enough.