Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (NSManagedObjectContext *)managedObjectContext {
- if (managedObjectContext != nil) {
- return managedObjectContext;
- }
- NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
- if (coordinator != nil) {
- managedObjectContext = [TICDSSynchronizedManagedObjectContext new];
- [managedObjectContext setPersistentStoreCoordinator: coordinator];
- }
- return managedObjectContext;
- }
- - (NSManagedObjectModel *)managedObjectModel {
- if (managedObjectModel != nil) {
- return managedObjectModel;
- }
- // managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
- NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"EntryDatabase" ofType:@"momd"];
- NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
- managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
- return managedObjectModel;
- }
- - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
- if (persistentStoreCoordinator != nil) {
- return persistentStoreCoordinator;
- }
- NSString *storePath = [[self applicationDocumentsDirectory]
- stringByAppendingPathComponent:@"CoreDataStore.sqlite"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- // If the expected store doesn't exist, copy the default store.
- NSLog(@"file exists at path: %@, %i", storePath, [fileManager fileExistsAtPath:storePath]);
- if (![fileManager fileExistsAtPath:storePath]) {
- NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"CoreDataStore" ofType:@"sqlite"];
- if (defaultStorePath) {
- [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
- }
- }
- persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
- NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
- NSError *error;
- NSDictionary *pscOptions = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
- [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption,
- nil];
- if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
- configuration:nil
- URL:storeUrl
- options:pscOptions
- error:&error]) {
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- }
- return persistentStoreCoordinator;
- }
- - (BOOL)checkForMigration
- {
- BOOL migrationSuccess = NO;
- NSString *storeSourcePath = [[self applicationDocumentsDirectory]
- stringByAppendingPathComponent:@"CoreDataStoreNew.sqlite"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:storeSourcePath]) {
- //Version 2 SQL has not been created yet, so the source is still version 1...
- storeSourcePath = [[self applicationDocumentsDirectory]
- stringByAppendingPathComponent:@"CoreDataStore.sqlite"];
- }
- NSURL *storeSourceUrl = [NSURL fileURLWithPath: storeSourcePath];
- NSError *error = nil;
- NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator
- metadataForPersistentStoreOfType:NSSQLiteStoreType
- URL:storeSourceUrl
- error:&error];
- if (sourceMetadata) {
- NSString *configuration = nil;
- NSManagedObjectModel *destinationModel = [self.persistentStoreCoordinator managedObjectModel];
- //Our Source 1 is going to be incompatible with the Version 2 Model, our Source 2 won't be...
- BOOL pscCompatible = [destinationModel isConfiguration:configuration compatibleWithStoreMetadata:sourceMetadata];
- NSLog(@"Is the STORE data COMPATIBLE? %@", (pscCompatible==YES) ?@"YES" :@"NO");
- if (pscCompatible == NO) {
- migrationSuccess = [self performMigrationWithSourceMetadata:sourceMetadata toDestinationModel:destinationModel];
- }
- }
- else {
- NSLog(@"checkForMigration FAIL - No Source Metadata! nERROR: %@", [error localizedDescription]);
- }
- return migrationSuccess;
- }
- - (BOOL)performMigrationWithSourceMetadata :(NSDictionary *)sourceMetadata
- toDestinationModel:(NSManagedObjectModel *)destinationModel
- {
- BOOL migrationSuccess = NO;
- //Initialise a Migration Manager...
- NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil
- forStoreMetadata:sourceMetadata];
- //Perform the migration...
- if (sourceModel) {
- NSMigrationManager *standardMigrationManager = [[NSMigrationManager alloc]
- initWithSourceModel:sourceModel
- destinationModel:destinationModel];
- NSArray *mappingModelNames = [NSArray arrayWithObjects:@"StepOne", @"StepTwo", nil];
- NSDictionary *sourceStoreOptions = nil;
- NSString *destinationStorePath = [[self applicationDocumentsDirectory]
- stringByAppendingPathComponent:@"CoreDataStoreNew.sqlite"];
- NSURL *destinationStoreURL = [NSURL fileURLWithPath: destinationStorePath];
- NSString *destinationStoreType = NSSQLiteStoreType;
- NSDictionary *destinationStoreOptions = nil;
- for (NSString *mappingModelName in mappingModelNames) {
- NSError *error;
- NSURL *fileURL = [[NSBundle mainBundle] URLForResource:mappingModelName withExtension:@"cdm"];
- NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:fileURL];
- migrationSuccess = [standardMigrationManager migrateStoreFromURL:destinationStoreURL
- type:NSSQLiteStoreType
- options:sourceStoreOptions
- withMappingModel:mappingModel
- toDestinationURL:destinationStoreURL
- destinationType:destinationStoreType
- destinationOptions:destinationStoreOptions
- error:&error];
- NSLog(@"Error: %@", error);
- }
- }
- return migrationSuccess;
- }
- if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
- configuration:nil
- URL:storeUrl
- options:pscOptions
- error:&error]) {
Add Comment
Please, Sign In to add comment