code_junkie

Manual Core Data schema migration without “document changed” warning?

Nov 13th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. if(upgradeNeeded) {
  2. NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:VUIModelBundles() orStoreMetadata:meta];
  3.  
  4. if(sourceModel == nil) {
  5. *error = [NSError errorWithDomain:VUIErrorDomainn ode:VUICoreDataErrorCode localizedReason:BWLocalizedString(@"Unable to find original data model for project.")];
  6. return NO;
  7. }
  8.  
  9. NSManagedObjectModel *destinationModel = [self managedObjectModel];
  10.  
  11. NSMigrationManager *migrationManager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel];
  12. NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles:VUIModelBundles() forSourceModel:sourceModel destinationModel:destinationModel];
  13. if(mappingModel == nil) {
  14. *error = [NSError errorWithDomain:VUIErrorDomain code:VUICoreDataErrorCode localizedReason:BWLocalizedString(@"Unable to find mapping model to convert project to most recent project format.")];
  15. return NO;
  16. }
  17.  
  18. @try {
  19. //move file to backup
  20. NSAssert([url isFileURL], @"store url is not a file URL");
  21.  
  22. NSString *tmpPath = [NSString tempFilePath];
  23. id storeType = [meta objectForKey:NSStoreTypeKey];
  24. if(![migrationManager migrateStoreFromURL:url
  25. type:storeType
  26. options:storeOptions
  27. withMappingModel:mappingModel
  28. toDestinationURL:[NSURLfileURLWithPath:tmpPath]
  29. destinationType:storeType
  30. destinationOptions:storeOptions
  31. error:error]) {
  32.  
  33. return NO;
  34. } else {
  35. //replace old with new
  36. if(![[NSFileManager defaultManager] removeItemAtPath:[url path] error:error] ||
  37. ![[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:[url path] error:error]) {
  38. return NO;
  39. }
  40.  
  41. // update document file modification date to prevent warning (#292)
  42. NSDate *newModificationDate = [[[NSFileManager defaultManager] fileAttributesAtPath:[url path] traverseLink:NO] bjectForKey:NSFileModificationDate];
  43. [self setFileModificationDate:newModificationDate];
  44. }
  45. }
  46. @finally {
  47. [migrationManager release];
  48. }
  49. }
  50. }
  51.  
  52. return [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error];
Add Comment
Please, Sign In to add comment