How to simply delete old kernel data and rebuild a new one?

I tried porting coreData to the new version, I follow this step:

Add a new version of the model (select the name .xcdatamodeld, then Editor-> Add model versions) before making any changes if you already have an application presented in the App Store that uses an earlier version of the model.

Then add a new file from the "Basic data" tab, such as "Select a display model", Source model (version of the model that the submitted application uses) Target model (version of the model in which you made changes)

a source

But my data is mostly related to images and applications, because it takes up a lot of memory. Therefore, I want to delete the old data model and its data and create empty new model data when the user updates his application. How to do it?

+5
source share
1 answer

When changing the data model, you can simply check which model has the database file. If it is not new, delete the file specified in StoreCoordinator using NSFileManager and run StoreCoordinater and NSManagedContext again to create a new one.

Something like this (untested code):

 var error: NSError var applicationDocumentsDirectory: NSURL = NSFileManager.defaultManager().URLsForDirectory(NSDocumentDirectory, inDomains:NSUserDomainMask).lastObject let storeURL: NSURL = applicationDocumentsDirectory.URLByAppendingPathComponent("Database.sqlite") NSFileManager.defaultManager().removeItemAtPath(storeURL.path, error) 

If the model has not changed, you need to save the update information anywhere. A text file in the database itself or in UserDefaults. You just need a flag to check if the database has been updated / cleared.

Then you can also delete the database as described above, or get all the objects and delete them.

+3
source

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


All Articles