Skip to content

Instantly share code, notes, and snippets.

@fechu
Created January 16, 2014 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fechu/8455540 to your computer and use it in GitHub Desktop.
Save fechu/8455540 to your computer and use it in GitHub Desktop.
Updater class to migrate to a new CoreData model
#import "Updater.h"
#import "SemesterOld.h"
#import "Semester.h"
#import "Fach.h"
#import "Subject.h"
@interface Updater ()
- (void)setupOldStack;
- (void)setupNewStack;
/**
Does the whole migration.
*/
- (void)migrate;
@end
@implementation Updater {
// Old Core Data stack
NSManagedObjectContext *oldContext;
NSPersistentStoreCoordinator *oldStoreCoordinator;
NSManagedObjectModel *oldObjectModel;
NSURL *oldDatabaseFile;
// New Core Data Stack
NSManagedObjectContext *newContext;
NSPersistentStoreCoordinator *newStoreCoordinator;
NSManagedObjectModel *newObjectModel;
NSURL *newDatabaseFile;
}
- (void)update
{
// URLs of the old and the new (temporary) database files.
NSFileManager *manager = [NSFileManager defaultManager];
newDatabaseFile = [NSURL fileURLWithPath: [[manager libraryDirectory] stringByAppendingPathComponent: @"Database.sqlite"]];
oldDatabaseFile = [NSURL fileURLWithPath: [[manager documentsDirectory] stringByAppendingPathComponent: @"Database.sqlite"]];
// If the old database file does not exist, just skip it!
if (![manager fileExistsAtPath:[oldDatabaseFile path]]) {
return;
}
// Setup both stacks.
[self setupOldStack];
[self setupNewStack];
[self migrate];
}
#pragma mark Migration
- (void)migrate
{
// We migrate only the data that the user still needs.
NSArray *oldSemesters = [SemesterOld MR_findAllInContext:oldContext];
// Create new Semesters
for (SemesterOld *semester in oldSemesters) {
Semester *newSemester = [Semester MR_createInContext:newContext];
newSemester.name = semester.name;
// ...
}
[newContext save:nil];
}
#pragma mark Setup
- (void)setupNewStack
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Database2" withExtension:@"momd"];
newObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
NSError *error;
newStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:newObjectModel];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![newStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:newDatabaseFile
options:options error:&error]) {
NSLog(@"Error with lodaing persistentstore: %@ , Userinfo: %@", error , [error userInfo]);
}
newContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:newStoreCoordinator];
}
- (void)setupOldStack
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Database1" withExtension:@"momd"];
oldObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
NSError *error;
oldStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:oldObjectModel];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![oldStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:oldDatabaseFile
options:options error:&error]) {
NSLog(@"Error with lodaing persistentstore: %@ , Userinfo: %@", error , [error userInfo]);
}
oldContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:oldStoreCoordinator];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment