首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取NSManagedObjectModel版本

获取NSManagedObjectModel版本
EN

Stack Overflow用户
提问于 2015-08-11 09:22:47
回答 1查看 641关注 0票数 0

我希望看到NSManagedObjectModel的版本,所以在更新它时,我希望删除所有托管对象,并从服务器下载所有记录,并重新填充本地持久存储。

知道怎么看最新版本的模型吗?

NSManagedObjectModel类引用

  • entityVersionHashesByName
  • versionIdentifiers

但我不认为这些是我所需要的。

EN

回答 1

Stack Overflow用户

发布于 2015-08-11 09:46:35

您可以通过以下方式获得该版本:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31937913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档