How to make easy CoreData migration to Swift

I am trying to perform the first easy migration of CoreData. I read two guides on easy migration. Both add code to the CoreDataStack class, modifying variables like NSPersistentStoreCoordinator, and add:

let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] 

My problem is that I have a well-functioning application using CoreData, but I don't have this class or anything like that. My problem is , why do these projects assume that I have this class, and can I achieve my easy migration without it? If not, how to add it?

Additional information if necessary

In September, I created an application using CoreData. This was my first time using CoreData, and I followed this Ray Wenderlich tutorial . It did a great job, I finished the application and it is now in the store. Now I would like to make some changes to the application, which involve the new CoreData attributes and several new objects. I read that I need to configure a new version of the model.

I found the Ray Wenderlich guide , but it uses this CoreDataStack.swift file, which I don't have:

Coredata class

It's hard to say that I am installing CoreData using my manual, and it does not include this file! Then I move on to migration, and they suggest that I have.

I went looking for another lightweight migration method, found this alternative, and it also refers to code that I never embedded in my CoreData:

 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail. // Create the coordinator and store var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyLog.sqlite") var error: NSError? = nil var failureReason = "There was an error creating or loading the application saved data." if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil { coordinator = nil // Report any error we got. var dict = [String: AnyObject]() dict[NSLocalizedDescriptionKey] = "Failed to initialize the application saved data" dict[NSLocalizedFailureReasonErrorKey] = failureReason dict[NSUnderlyingErrorKey] = error error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) // Replace this with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog("Unresolved error \(error), \(error!.userInfo)") abort() } 

So, I read the manuals and understood 90% of the lessons. I just need someone to take a look at this original CoreData tutorial and tell me where, if I don't have a CoreData class, I would add some light code, like this:

 let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] 
+5
source share
1 answer

Transfer options must be used in a call that adds persistent storage to the persistent storage coordinator. You can easily find this line of code by searching addPersistentStoreWithType .

 try coordinator!.addPersistentStoreWithType( NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions) 

Most likely, your Core Data stack is in the AppDelegate class, but no matter where it is, you need to add migration options.

+8
source

Source: https://habr.com/ru/post/1241092/


All Articles