daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (NSManagedObjectContext *)managedObjectContext {
  2.  
  3.     if (managedObjectContext != nil) {
  4.         return managedObjectContext;
  5.     }
  6.  
  7.     NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  8.     if (coordinator != nil) {
  9.         managedObjectContext = [TICDSSynchronizedManagedObjectContext new];
  10.         [managedObjectContext setPersistentStoreCoordinator: coordinator];
  11.     }
  12.     return managedObjectContext;
  13. }
  14. - (NSManagedObjectModel *)managedObjectModel {
  15.  
  16.     if (managedObjectModel != nil) {
  17.         return managedObjectModel;
  18.     }
  19.  
  20.     //    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
  21.     NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"EntryDatabase" ofType:@"momd"];
  22.     NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
  23.     managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  24.  
  25.     return managedObjectModel;
  26. }
  27. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  28.  
  29.     if (persistentStoreCoordinator != nil) {
  30.         return persistentStoreCoordinator;
  31.     }
  32.  
  33.     NSString *storePath = [[self applicationDocumentsDirectory]
  34.                            stringByAppendingPathComponent:@"CoreDataStore.sqlite"];
  35.  
  36.     NSFileManager *fileManager = [NSFileManager defaultManager];
  37.     // If the expected store doesn't exist, copy the default store.
  38.     NSLog(@"file exists at path: %@, %i", storePath, [fileManager fileExistsAtPath:storePath]);
  39.     if (![fileManager fileExistsAtPath:storePath]) {
  40.         NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"CoreDataStore" ofType:@"sqlite"];
  41.         if (defaultStorePath) {
  42.             [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
  43.         }
  44.     }
  45.  
  46.     persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  47.  
  48.     NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
  49.     NSError *error;
  50.     NSDictionary *pscOptions = [NSDictionary dictionaryWithObjectsAndKeys:
  51.                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  52.                                 [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption,
  53.                                 nil];
  54.  
  55.     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
  56.                                                   configuration:nil
  57.                                                             URL:storeUrl
  58.                                                         options:pscOptions
  59.                                                           error:&error]) {
  60.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  61.     }
  62.  
  63.     return persistentStoreCoordinator;
  64. }
  65.  
  66. - (BOOL)checkForMigration
  67. {
  68.     BOOL migrationSuccess = NO;
  69.     NSString *storeSourcePath = [[self applicationDocumentsDirectory]
  70.                                  stringByAppendingPathComponent:@"CoreDataStoreNew.sqlite"];
  71.     NSFileManager *fileManager = [NSFileManager defaultManager];
  72.  
  73.     if (![fileManager fileExistsAtPath:storeSourcePath]) {
  74.         //Version 2 SQL has not been created yet, so the source is still version 1...
  75.         storeSourcePath = [[self applicationDocumentsDirectory]
  76.                            stringByAppendingPathComponent:@"CoreDataStore.sqlite"];
  77.     }
  78.  
  79.     NSURL *storeSourceUrl = [NSURL fileURLWithPath: storeSourcePath];
  80.     NSError *error = nil;
  81.     NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator
  82.                                     metadataForPersistentStoreOfType:NSSQLiteStoreType
  83.                                     URL:storeSourceUrl
  84.                                     error:&error];
  85.     if (sourceMetadata) {
  86.         NSString *configuration = nil;
  87.         NSManagedObjectModel *destinationModel = [self.persistentStoreCoordinator managedObjectModel];
  88.  
  89.         //Our Source 1 is going to be incompatible with the Version 2 Model, our Source 2 won't be...
  90.         BOOL pscCompatible = [destinationModel isConfiguration:configuration compatibleWithStoreMetadata:sourceMetadata];
  91.         NSLog(@"Is the STORE data COMPATIBLE? %@", (pscCompatible==YES) ?@"YES" :@"NO");
  92.  
  93.         if (pscCompatible == NO) {
  94.             migrationSuccess = [self performMigrationWithSourceMetadata:sourceMetadata toDestinationModel:destinationModel];
  95.         }
  96.     }
  97.     else {
  98.         NSLog(@"checkForMigration FAIL - No Source Metadata! nERROR: %@", [error localizedDescription]);
  99.     }
  100.     return migrationSuccess;
  101. }
  102.  
  103.  
  104. - (BOOL)performMigrationWithSourceMetadata :(NSDictionary *)sourceMetadata
  105.                          toDestinationModel:(NSManagedObjectModel *)destinationModel
  106. {
  107.  
  108.  
  109.     BOOL migrationSuccess = NO;
  110.     //Initialise a Migration Manager...
  111.     NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil
  112.                                                                     forStoreMetadata:sourceMetadata];
  113.     //Perform the migration...
  114.     if (sourceModel) {
  115.         NSMigrationManager *standardMigrationManager = [[NSMigrationManager alloc]
  116.                                                         initWithSourceModel:sourceModel
  117.                                                         destinationModel:destinationModel];
  118.  
  119.         NSArray *mappingModelNames = [NSArray arrayWithObjects:@"StepOne", @"StepTwo", nil];
  120.         NSDictionary *sourceStoreOptions = nil;
  121.  
  122.         NSString *destinationStorePath = [[self applicationDocumentsDirectory]
  123.                                           stringByAppendingPathComponent:@"CoreDataStoreNew.sqlite"];
  124.  
  125.         NSURL *destinationStoreURL = [NSURL fileURLWithPath: destinationStorePath];
  126.  
  127.         NSString *destinationStoreType = NSSQLiteStoreType;
  128.  
  129.         NSDictionary *destinationStoreOptions = nil;
  130.  
  131.         for (NSString *mappingModelName in mappingModelNames) {
  132.  
  133.             NSError *error;
  134.  
  135.             NSURL *fileURL = [[NSBundle mainBundle] URLForResource:mappingModelName withExtension:@"cdm"];
  136.  
  137.             NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:fileURL];
  138.  
  139.             migrationSuccess = [standardMigrationManager migrateStoreFromURL:destinationStoreURL
  140.                                                                         type:NSSQLiteStoreType
  141.                                                                      options:sourceStoreOptions
  142.                                                             withMappingModel:mappingModel
  143.                                                             toDestinationURL:destinationStoreURL
  144.                                                              destinationType:destinationStoreType
  145.                                                           destinationOptions:destinationStoreOptions
  146.                                                                        error:&error];
  147.  
  148.             NSLog(@"Error: %@", error);
  149.         }
  150.  
  151.     }
  152.  
  153.     return migrationSuccess;
  154.  
  155. }
  156.    
  157. if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
  158.                                               configuration:nil
  159.                                                         URL:storeUrl
  160.                                                     options:pscOptions
  161.                                                       error:&error]) {
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top