Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename persistentstore coordinator URL

i have a (proably) simple to fix issue with my coredata persistent store.

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 working with the project, and have forgotten to rename it.. now all my current test devices (4) were in use for over 2 month, using the application, collecting a lot of data and storing it with a stupid url ^^

UPDATE i did some research on migration of persistent stores 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?

like image 353
Sebastian Flückiger Avatar asked Apr 18 '12 15:04

Sebastian Flückiger


1 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];

}

afterwards i just changed the appendURL to database.sqli in my appDelegate. works like a charm :)

like image 142
Sebastian Flückiger Avatar answered Sep 28 '22 03:09

Sebastian Flückiger