Rename persistentstore coordinator url

I have a (possibly) simple fix problem in my coredata persistent storage.

i created it with

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator != nil) { return persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"dummyURL.sqlite"]; NSError *error = nil; persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return persistentStoreCoordinator; } 

using the URL dummyURL.sqlite

I did this on the first day of working with the project and forgot to rename it. Now all my current test devices (4) have been used for more than 2 months using the application, collecting a lot of data and saving it with a silly url ^^

UPDATE I did some research on persistent storage migration and wrote this function:

 -(void)migrate{ NSPersistentStoreCoordinator *psc = [self.dataHandler.managedObjectContext persistentStoreCoordinator]; NSURL *oldURL = [psc URLForPersistentStore:[[psc persistentStores]objectAtIndex:0]]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSURL *newURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"]; NSError *error = nil; NSPersistentStore *oldStore = [psc persistentStoreForURL:oldURL]; NSPersistentStore *newStore = [psc migratePersistentStore:sqliStoreOld toURL:newURL options:nil withType:NSSQLiteStoreType error:&error]; } 

QUESTION 1 will this work or will i lose some data with that?

QUESTION 2 afterwards will i just have to change the appendString in my persistenstore function?

+6
source share
2 answers

I managed to solve it myself using the migratePersistentStore function:

 -(void)migrate{ NSPersistentStoreCoordinator *psc = [self.dataHandler.managedObjectContext persistentStoreCoordinator]; NSURL *oldURL = [psc URLForPersistentStore:[[psc persistentStores]objectAtIndex:0]]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSURL *newURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"]; NSError *error = nil; NSPersistentStore *oldStore = [psc persistentStoreForURL:oldURL]; NSPersistentStore *newStore = [psc migratePersistentStore:sqliStoreOld toURL:newURL options:nil withType:NSSQLiteStoreType error:&error]; } 

after that I just changed appendURL to database.sqli in my appDelegate application. works like a charm :)

+3
source

I would recommend creating a second persistent storage with a new URL and adding a button somewhere that will copy all the data that you have into the new one. Make sure you verify that all your data is in the new persistent storage before you delete the old one from the application.

0
source

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


All Articles