[英]How to get Managed Object context from RestKit 0.20
我正在使用Reskit 0.20,並且已經在我的appDelegate.m中編寫了此代碼
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self restKitLaunch];
return YES;
}
-(void) restKitLaunch {
// Initialize RestKit
NSURL *baseURL = [NSURL URLWithString:@"http://barcelonaapi.marcpous.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
// Initialize managed object model from bundle
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
// Initialize managed object store
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;
// Complete Core Data stack initialization
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"BusDB.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
RKEntityMapping *lineListMapping = [RKEntityMapping mappingForEntityForName:@"Line" inManagedObjectStore:managedObjectStore];
lineListMapping.identificationAttributes = @[ @"line" ];
[lineListMapping addAttributeMappingsFromArray:@[@"line", @"origin", @"destination"]];
RKResponseDescriptor *lineListResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:lineListMapping
method:RKRequestMethodGET
pathPattern:@"/bus/lines.json"
keyPath:@"data.tmbs"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
];
[objectManager addResponseDescriptor:lineListResponseDescriptor];
}
一切正常,我的sqlite數據庫運行良好。 問題是我已經創建了其他實體,並且我想以ManagedObjectContext的常規方式添加對象。 如何通過此代碼創建靜態方法以獲取托管對象上下文? 謝謝你的幫助
好的,我可以叫這個:
NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
從任何viewController,當我想要NSManagedObjectContext。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.