Tuesday, August 6, 2013

“Can't find model for source store” - Core Data Migration



I was tearing my hair out over the "Can't find model for source store"  && "Cannot create an NSMigrationManager with a nil source model".error for a whole day.

Problem Description :

I have a app Zitrr Camera 1.1 which is already on app store. Now in Version 1.3 of the app , i am trying to run core data migration on it but it seems to be unable to find the source object model no matter what. I searched google, stackoverflow tried many things, but no success.

Version 1.1 Core Data Model : (Old One)




Version 1.3 Core Data Model : (Updated One)





NOTE : I have created only one Entity "ZNCAsset" in my Zitrr Camera Project.

What i Tried:

-  Core Data Versioning and Migration to do it automatically. I created a new version of the model and set it as the current version and called addPersistentStoreWithType:configuration:URL:options:error: with NSMigratePersistentStoresAutomaticallyOption, NSInferMappingModelAutomaticallyOption. but no luck.

- Since the automatic migration seems not to work in any way,  I tried manual migration . I extracted the metadata and checked if model is compatible. then find the suitable source moderl for the store via mergedModelFromBundles:forStoreMetadata:. Same as before Source model still nil.

i can delete my old datamodel file  and start fresh as need to migrate my Version 1.1 customer's data.

                                                             Stuck :( !

Solution For Above Problem, "From No Luck to Luck"!

I started debugging at version 1.3. when i Printed metadata of store by using NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType
  URL:url error:&error it looked like :





But i created only one Entity "ZNCAsset" in Zitrr Camera. From where these TKBlog, TKPhoto,TKChat ... TKUser etc came from? i don't have any datamodel  in Version 1.3 that describes Entities like TKBlog, TKPhoto etc. So I Opened my Code of Version 1.1 and searched their and 

                                                             BINGO!
    
Actually In Version 1.1 i Used thirdParty Library "TumblrAPI". Which had a file "TumblrKit.xcdatamodeld" , which have Entity like  TKBlog, TKPhoto etc Entity. As we dropped Support of Tumblr in  Version 1.3 , So i Deleted All Files From Projected related to Tumblr API  including "TumblrKit.xcdatamodeld". So now I added back only "TumblrKit.xcdatamodeld" file  to version 1.3 and run it over version 1.1 and now its just started working fine like old days.


Why This Problem Occured :


this is because side effect of code

NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil forStoreMetadata:sourceMetadata];

which  will create a model containing entities from my main model as well as my other model  linked to the application (here Tumblr Model).  Above Code has Pulled All extra Entity From Tumblr in Version 1.1 and stored it As a metadata of Store, but now when in New Version 1.3 Source Model is Created Using 

NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil forStoreMetadata:sourceMetadata];

As  Source Metadata have that tumblr Entity information, but  don't have model ("TumblrKit.xcdatamodeld")file in Version 1.3 as (Tumblr data model file is got deleted in this version). So it not able to create source model from source store.






6 comments: