简体   繁体   English

RestKit与核心数据:将NSPersistentStore与RestKit RKObjectManager一起使用

[英]RestKit with Core Data: Use NSPersistentStore with RestKit RKObjectManager

I am using RestKit with Core Data. 我正在使用带有核心数据的RestKit。 Till now I initialized RestKit as following: 到现在为止,我按照以下方式初始化RestKit:

[...]
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURLString:@"http://URL"];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"database.sql"];
[...]

without this xcode auto-generated Core Data properties: 没有此xcode自动生成的Core Data属性:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

Fetching data worked through calling objectsWithFetchRequest: in NSManagedObject. 通过调用NSManagedObject中的objectsWithFetchRequest:来获取数据。 Now I would like to use NSFetchingResultsController and therefore I need at least such a managedObjectContext. 现在,我想使用NSFetchingResultsController,因此至少需要这样的ManagedObjectContext。 But wherefrom do I get this managedObjectContext? 但是我从哪里得到这个managedObjectContext? My first idea was: from the auto-generated managedObjectContext in AppDelegate.m, but this 3 properties managedObjectContext, managedObjectModel and persistentStoreCoordinator are all depending on each other. 我的第一个想法是:从AppDelegate.m中自动生成的managedObjectContext,但是这3个属性ManagedObjectContext,managedObjectModel和persistentStoreCoordinator都相互依赖。

How can I "link" RestKit to Core Data correctly? 如何将RestKit正确“链接”到核心数据? How can I "link" a persistentStoreCoordinator to RestKit and the RKObjectManager? 如何将“ persistentStoreCoordinator”链接到RestKit和RKObjectManager?

You are already linking RestKit and Core Data with that code. 您已经使用该代码链接RestKit和Core Data。 Now, if I understand the question, you just need to be able to get access to the managed object context. 现在,如果我理解了这个问题,则只需要能够访问托管对象上下文即可。 Try creating a "Core Data Manager" object that contains all of the code required for the context, model and store coordinator with those properties. 尝试创建一个“ Core Data Manager”对象,该对象包含上下文,模型以及使用这些属性存储协调器所需的所有代码。 Then any of your classes that require that code can just inherit from the core data manager. 然后,您需要该代码的任何类都可以直接从核心数据管理器继承。

Found the answer at http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/ http://mobile.tutsplus.com/tutorials/iphone/advanced-restkit-development_iphone-sdk/找到了答案

RKManagedObjectStore – The object store wraps the initialization and configuration of internal Core Data classes including NSManagedObjectModel, NSPersistentStoreCoordinator, and NSManagedObjectContext. RKManagedObjectStore –对象存储包装了内部Core Data类的初始化和配置,包括NSManagedObjectModel,NSPersistentStoreCoordinator和NSManagedObjectContext。

RestKit handles the "link" to Core Data quiet autonomous. RestKit处理“链接”到核心数据的静默自治。 There are no linkings to do. 没有链接可做。

Getting a NSFetchingResultsController is possible through a NSManagedObject calling the fetchAllSortedBy or a similar method in NSManagedObject+ActiveRecord.heg 通过调用fetchAllSortedBy的NSManagedObject或NSManagedObject + ActiveRecord.heg中的类似方法,可以获取NSFetchingResultsController。

fetchedResultsController = [Customer fetchAllSortedBy:@"surname" ascending:YES withPredicate:nil groupBy:@"surname"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM