Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 5.64 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Core Data - Default Migration ( Manual )
  2. - (NSManagedObjectModel *)managedObjectModel
  3. {
  4.     if (__managedObjectModel != nil)
  5.     {
  6.         return __managedObjectModel;
  7.     }
  8.     NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"App" withExtension:@"momd"];
  9.     __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
  10.     return __managedObjectModel;
  11. }
  12.        
  13. /**
  14.  Returns the persistent store coordinator for the application.
  15.  If the coordinator doesn't already exist, it is created and the application's store added to it.
  16.  */
  17. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  18. {
  19.     if (__persistentStoreCoordinator != nil)
  20.     {
  21.         return __persistentStoreCoordinator;
  22.     }
  23.  
  24.     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"App.sqlite"];
  25.     NSError *error = nil;
  26.  
  27.     __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  28.  
  29.     NSLog(@"Which Current Version is our .xcdatamodeld file set to? %@", [[self managedObjectModel] versionIdentifiers]);
  30.     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:  
  31.                              [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  32. //Commented for manual migration [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,  
  33.                              nil];  
  34.  
  35.  
  36.     if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
  37.     {
  38.  
  39.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  40.         abort();
  41.     }    
  42.  
  43.     return __persistentStoreCoordinator
  44. }
  45.        
  46. reason=Can't find mapping model for migration
  47.        
  48. ## EDIT ##
  49.        
  50. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
  51. {
  52.     if (__persistentStoreCoordinator != nil)
  53.     {
  54.         return __persistentStoreCoordinator;
  55.     }
  56.  
  57.     NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestData.sqlite"];
  58.  
  59.     NSError *error = nil;
  60.     __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  61.  
  62.     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:  
  63.                              [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  64.                                                        nil];
  65.  
  66.     if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
  67.     {
  68.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  69.         abort();
  70.     }    
  71.  
  72.     return __persistentStoreCoordinator;
  73. }
  74.        
  75. Report = "(<NSEntityDescription: 0x1708d0>) name Report, managedObjectClassName Report, renamingIdentifier Report, isAbstract 0, superentity name (null), properties {n    action = "(<NSAttributeDescription: 0x170a30>), name action, isOptional 1, isTransient 0, entity Report, renamingIdentifier action, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0";n    data = "(<NSAttributeDescription: 0x1709e0>), name data, isOptional 1, isTransient 0, entity Report, renamingIdentifier data, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)";n    reportId = "(<NSAttributeDescription: 0x170a80>), name reportId, isOptional 1, isTransient 0, entity Report, renamingIdentifier reportId, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0";n    timestamp = "(<NSAttributeDescription: 0x170ad0>), name timestamp, isOptional 1, isTransient 0, entity Report, renamingIdentifier timestamp, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)";n    type = "(<NSAttributeDescription: 0x170990>), name type, isOptional 1, isTransient 0, entity Report, renamingIdentifier type, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0";n}, subentities {n}, userInfo {n}, versionHashModifier (null)";
  76. }, fetch request templates {
  77. }, reason=Can't find mapping model for migration}, {
  78.        
  79. NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"cdm"];
  80.  NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:fileURL];
  81.  
  82.  NSArray *newEntityMappings = [NSArray arrayWithArray:mappingModel.entityMappings];
  83.  for (NSEntityMapping *entityMapping in newEntityMappings) {
  84.  
  85. [entityMapping setSourceEntityVersionHash:[sourceModel.entityVersionHashesByName     valueForKey:entityMapping.sourceEntityName]];
  86. [entityMapping setDestinationEntityVersionHash:[destinationModel.entityVersionHashesByName valueForKey:entityMapping.destinationEntityName]];
  87.         }
  88. mappingModel.entityMappings = newEntityMappings;
  89.  
  90.         BOOL ok = [migrationManager migrateStoreFromURL:sourceStoreURL
  91.                                                    type:sourceStoreType
  92.                                                 options:nil
  93.                                        withMappingModel:mappingModel
  94.                                        toDestinationURL:destinationStoreURL
  95.                                         destinationType:destinationStoreType
  96.                                      destinationOptions:nil
  97.                                                   error:&error];