我不确定我是否真正了解我正在做的事情,但我的问题是我无法设置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是否需要它自己的持久存储,它将与"默认"同步。一?或者它是相同的存储空间而我只是初始化错误?
提前致谢!
答案 0 :(得分:0)
我也在我的应用程序中使用RestKit。为了管理/存储除RestKit数据之外的应用程序数据,我实现了您尝试过的反向方法。我首先配置了restKit,并将该上下文用于其他数据。
我创建了一个RestKitHandler类来管理所有基于restKit的活动,我在应用程序的所有其他部分使用该类的单例 - [RestKitHandler sharedInstance]
实例。
在RKObjectManager
班级
RestKitHandler
的强大属性
@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