【问题标题】:Creating RKManagedObjectStore based on Existing Core Data Store基于现有核心数据存储创建 RKManagedObjectStore
【发布时间】:2015-01-08 11:43:16
【问题描述】:

我不确定我是否真的了解我在做什么,但我的问题是我无法设置 RKManagedObjectStore 以同时使用 Core Data 和 RestKit。我尝试了以下方法:

NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.MYAPPLICATIONURL"];
storeURL = [storeURL URLByAppendingPathComponent:@"MYAPPLICATIONNAME.sqlite"];
NSError *error = nil;
manager.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[[CoreDataHandler instance] managedObjectModel]];
[manager.managedObjectStore addSQLitePersistentStoreAtPath:[storeURL absoluteString] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];

if (error != nil) {
    NSLog(@"\nSerious object store error: %@", error);
    return;
} else {
    [manager.managedObjectStore createManagedObjectContexts];
}

[[CoreDataHandler instance] managedObjectModel] 返回默认的 CoreData 对象模型。我没有更改 xcode 模板提供的初始化代码的任何内容。

我得到的错误是:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:MYURLTOSQLITEFILE

...

returned error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x7fc5aaf9f290 {reason=Failed to create file; code = 2} with userInfo dictionary {
    reason = "Failed to create file; code = 2";
}

所以我的问题是:我是否以正确的方式尝试这个? RestKit 是否需要它自己的持久存储,它将与“默认”存储同步?还是它是同一个存储,我只是初始化错误?

提前致谢!

【问题讨论】:

    标签: ios core-data restkit restkit-0.20


    【解决方案1】:

    我也在我的应用程序中使用 RestKit。为了管理/存储 RestKit 数据以外的应用程序数据,我实施了您尝试过的反向方法。我首先配置了 restKit 并将该上下文用于其他数据。

    我创建了一个 RestKitHandler 类来管理所有基于 restKit 的活动,并且我在应用程序的所有其他部分中使用该类的单例 - [RestKitHandler sharedInstance] 实例。

    RestKitHandler 类中创建RKObjectManager 的强属性,

    @property (nonatomic, strong) RKObjectManager *objectManager;
    

    并在你的RestKitHandler.m 中配置restKit 之类的,

    // Configure RestKit
    NSString *const baseURL = @"http://baseURL";
    
    self.objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURL]];
    [_objectManager.HTTPClient setDefaultHeader:@"KEY" value:@"value"];
    _objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
    
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    
    // Set up persistance storage and create managed object context to connect RestKit with CoreData
    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    _objectManager.managedObjectStore = managedObjectStore;
    
    [managedObjectStore createPersistentStoreCoordinator];
    
    NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"YourApp.sqlite"];
    
    NSError *error;
    
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error];
    
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
    
    [managedObjectStore createManagedObjectContexts];
    
    // Configure a managed object cache to ensure we do not create duplicate objects
    managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
    

    通过这个方法你可以从objectManager实例中获取所有核心数据相关的对象,比如,

    [[[[RestKitHandler sharedInstance] objectManager] managedObjectStore] persistentStoreManagedObjectContext];
    
    OR
    
    [[[[RestKitHandler sharedInstance] objectManager] managedObjectStore] mainQueueManagedObjectContext];
    
    // Also all other objects like this
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 2013-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多