1 Reply
      Latest reply on Oct 25, 2019 10:23 PM by jrog86
      SeriouslyDoYouGuysEvenUseXcode Level 1 Level 1 (0 points)

        Hi,

         

        I tried to automatically migrate my database, I didn't get any errors, however all the existing Entitiy rows are missing after automatic migration.

        So I tried manual migration without much luck.

         

        Does anyone have a full example of a manual migration using NSMigrationManager.migrateStore?

        The only example I have found so fat is the objc.io one, but it doesn't use migrateStore.

         

        I am following the Core Data Programming Guide but the parameters are not really specified in their examples.

         

        I am having issues working out how to specify some of the parameters, particularly around what the sourceModel and destinationModel should actually point to.

        sourceVersion = "2.5.2" (My App Store Core Data Version)

        destinationVersion = "2.5.3" (Development version with lots of changes)

         

        sourceModelUrl = file:///Users/MyName/Library/Developer/CoreSimulator/Devices/38B3746B-9BDF-4641-9783-4FAAE00DA369/data/Containers/Bundle/Application/D94FAD26-A12D-4486-8E67-618778B7A80E/XXXXXX.app/APDataModel.momd/APDataModel%202.5.2.mom

        destinationModelUrl = file:///Users/MyName/Library/Developer/CoreSimulator/Devices/38B3746B-9BDF-4641-9783-4FAAE00DA369/data/Containers/Bundle/Application/D94FAD26-A12D-4486-8E67-618778B7A80E/XXXXXX.app/APDataModel.momd/APDataModel%202.5.3.mom

         

        sourceModel = an NSManagedObjectModel loaded from the above sourceModelUrl

        destinationModel = an NSManagedObjectModel loaded from the above destinationModelUrl

         

        coreDataManager.databaseURL = file:///Users/MyName/Library/Developer/CoreSimulator/Devices/38B3746B-9BDF-4641-9783-4FAAE00DA369/data/Containers/Data/Application/0A734ABB-99EC-4F39-9BCC-66E34A7BC497/Documents/APDataModel.sqlite

        coreDataManager.storeType = "SQLite"

         

        I tried using NSPersistentStoreCoordinator.metadataForPersistentStore and NSManagedObjectModel.mergedModel to get the sourceModel but that just gave me errors.

         

                guard let sourceModelUrl = coreDataManager.modelUrl( forVersion:sourceVersion),
                    let destinationModelUrl = coreDataManager.modelUrl( forVersion:destinationVersion) else
                {
                    return false
                }
        
                guard let sourceModel = sourceModel( forVersion:sourceVersion),
                      let destinationModel = destinationModel( forVersion:destinationVersion) else
                {
                    return false
                }
        
                guard let mappingModel = NSMappingModel( from:[Bundle.main],
                                                         forSourceModel:sourceModel,
                                                         destinationModel:destinationModel) else
                {
                    return false
                }
                let migrationManager = NSMigrationManager( sourceModel:sourceModel,
                                                           destinationModel:destinationModel)
                do
                {
                    try migrationManager.migrateStore( from:coreDataManager.databaseURL,
                                                       sourceType:coreDataManager.storeType,
                                                       options:[:],
                                                       with:mappingModel,
                                                       toDestinationURL:destinationModelUrl,
                                                       destinationType:coreDataManager.storeType,
                                                       destinationOptions:[:])
                }
                catch let error
                {
                    print("error in migration: \(error)")
                    return false
                }
        

         

        This throws an error which I can't really paste all of as it shows a lot of my data model which belongs to my client.

        However the interesting parts:

        CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null)

        38B3746B-9BDF-4641-9783-4FAAE00DA369/data/Containers/Data/Application/0A734ABB-99EC-4F39-9BCC-66E34A7BC497/Documents/APDataModel.sqlite options:{

            NSReadOnlyPersistentStoreOption = 1;

        } ... returned error Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={

             [ MY CORE DATA SCHEMA IS LISTED HERE, IT LOOKS LIKE VERSION 2.5.3 ]

        }, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary {

             [ MY CORE DATA SCHEMA IS LISTED HERE AGAIN, IT LOOKS LIKE VERSION 2.5.3 AGAIN ]

           };

            reason = "The model used to open the store is incompatible with the one used to create the store";

        }

         

        I am really stuck, have been working on this for days and any help would be really appreciated.

         

        Ps. Sorry about the crappy username, must have created it when having a bad Xcode day.