- (BOOL)migrationNeeded { return ! ( currentStoreModel == [objectModels count] ); } - (BOOL)migrateIfNeeded:(NSError**)error { if ( ! [self migrationNeeded] ) { return YES; } // Backup the store before we do anything NSString* backupFilePath = [[storeURL path] stringByAppendingString:@".backup"]; if( ![self overWriteCopy:[storeURL path] to:backupFilePath error:error] ) { return NO; } NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; NSInteger i; NSURL* tempURL = [NSURL fileURLWithPath:[[storeURL path] stringByAppendingString:@".temp"]]; for( i = currentStoreModel; i < [objectModels count] - 1; i++ ) { // Migrate the store to a temp file. // SunFlower.xml --> SunFlower.xml.temp BOOL migrationSuccess = [[self migrationManagerForIndex:i] migrateStoreFromURL:storeURL type:NSXMLStoreType options:opts withMappingModel:[self mappingModelForIndex:i] toDestinationURL:tempURL destinationType:NSXMLStoreType destinationOptions:opts error:error]; // Revert and return if: // 1.) The migration failed. // 2.) The temp file could not be copied over the original. // 3.) The temp file could not be deleted. if (! migrationSuccess || ! [self overWriteCopy:[tempURL path] to:[storeURL path] error:error] || ! [[NSFileManager defaultManager] removeItemAtPath:[tempURL path] error:error]) { // Revert to backup file and exit // Not passing an error because we want to propogate the previous error. [self overWriteCopy:backupFilePath to:[storeURL path] error:NULL]; return NO; } } return YES; }