我想查看NSManagedObjectModel
的版本,因此在更新时,我想删除所有托管对象,并从服务器下载所有记录并重新填充本地持久性存储。
知道如何查看最新版本的模型吗?
NSManagedObjectModel
班级参考
但我不认为这些是我需要的。
答案 0 :(得分:0)
您可以通过以下方式获取该版本:
NSSet *versionIdentifiers = [[self managedObjectModel] versionIdentifiers];
NSLog(@"Which Current Version is our .xcdatamodeld file set to? %@", versionIdentifiers);
- (BOOL)checkForMigration
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];
}
}
- (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];
//Retrieve the appropriate mapping model...
NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles: nil
forSourceModel: sourceModel
destinationModel: destinationModel];
if (mappingModel)
{
NSError *error = nil;
NSString *storeSourcePath = [[self applicationDocumentsDirectoryString] stringByAppendingPathComponent: @"SQL-FILE-V1"];
NSURL *storeSourceUrl = [NSURL fileURLWithPath: storeSourcePath];
NSString *storeDestPath = [[self applicationDocumentsDirectoryString] stringByAppendingPathComponent: @"SQL-File-V2.sqlite"];
NSURL *storeDestUrl = [NSURL fileURLWithPath: storeDestPath];
//Pass nil here because we don't want to use any of these options:
//NSIgnorePersistentStoreVersioningOption, NSMigratePersistentStoresAutomaticallyOption, or NSInferMappingModelAutomaticallyOption
NSDictionary *sourceStoreOptions = nil;
NSDictionary *destinationStoreOptions = nil;
migrationSuccess = [standardMigrationManager migrateStoreFromURL: storeSourceUrl
type: NSSQLiteStoreType
options: sourceStoreOptions
withMappingModel: mappingModel
toDestinationURL: storeDestUrl
destinationType: NSSQLiteStoreType
destinationOptions: destinationStoreOptions
error: &error];
NSLog(@"MIGRATION SUCCESSFUL? %@", (migrationSuccess == YES) ? @"YES" : @"NO");
}
}
else
{
//TODO: Error to user...
NSLog(@"checkForMigration FAIL - No Mapping Model found!");
abort();
}
return migrationSuccess;
} //END