RESTKit:对持久存储进行细微更改

Posted

技术标签:

【中文标题】RESTKit:对持久存储进行细微更改【英文标题】:RESTKit: Making minor changes to Persistent Store 【发布时间】:2014-05-22 17:22:48 【问题描述】:

如果向任何实体添加属性,我必须删除并重新安装应用程序,因为它会崩溃。我的印象是,下面的选项将允许我对持久存储进行微小的更改?

- (id)optionsForSqliteStore 
    return @
             NSInferMappingModelAutomaticallyOption: @YES,
             NSMigratePersistentStoresAutomaticallyOption: @YES
             ;


- (RKManagedObjectStore *)setupCoreDataWithRESTKit


    NSError * error;
    NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]];
    NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];


    [self.managedObjectStore createPersistentStoreCoordinator];

    NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * documentPath = [searchPaths objectAtIndex:0];
    NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"/App.sqlite" fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error];
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

    NSLog(@"Path: %@", [NSString stringWithFormat:@"%@/App%@.sqlite", documentPath, [TBSPersistence username]]);

    if(!persistentStore)
        NSLog(@"Failed to add persistent store: %@", error);
    

    [self.managedObjectStore createManagedObjectContexts];

    return self.managedObjectStore;


这是日志:

014-05-22 10:12:23.631 App[479:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to add persistent store with error: Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0x15ea8ee0 URL=file:///var/mobile/Applications/4BA83C9C-E488-4B3D-B6CC-2CC39CC41AC5/Documents/App.sqlite, metadata=
    NSPersistenceFrameworkVersion = 479;
    NSStoreModelVersionHashes =     
      // Here listed are all the Entities...
    ;
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "12CC0291-F86F-43B6-A762-873B304C5E6F";
    "_NSAutoVacuumLevel" = 2;
, reason=Can't find model for source store'

【问题讨论】:

【参考方案1】:

这取决于...

您需要对模型进行版本化,以便可以量化差异,并且您需要在自动迁移的范围内进行更改(查看文档,但您不能删除、更改数据类型......)。

【讨论】:

以上是关于RESTKit:对持久存储进行细微更改的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 2.0 删除 RKManagedObjectStore 但保留 NSManagedObjectModel

RestKit:在注销时重置持久存储后映射了 0 个对象

RestKit/CoreData 没有立即将关系保存到持久存储

我可以防止 RestKit+CoreData 覆盖本地实体更改吗?

使用 RestKit 0.2x 创建两个独立的持久存储

RESTkit,Core Data:在将对象传输到 Core Data 之前对其进行处理