How to get Managed Object context from RestKit 0.20

277 views Asked by At

I'm using Reskit 0.20 and i ve made this code in my 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];
}

Everything works fine and and my sqlite database works well. The problem is i've created other entity and i want to add object in the usual way with the managedObjectContext. How can i create a static method from this code to get back the managed object context ? Thanks for your help

1

There are 1 answers

0
Gregory Molette On

Ok find i can call this :

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;

from any viewController when i want the NSManagedObjectContext.