Reputation: 46228
I'm trying to map a CoreData Entity called Product
with a web service.
NSString *objName = @"Product";
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL: baseUrl];
NSManagedObjectContext *context = [[MContextManager sharedContextManager] managedObjectContext];
NSManagedObjectModel *managedObjectModel = [[context persistentStoreCoordinator] managedObjectModel];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[managedObjectStore createPersistentStoreCoordinator];
[managedObjectStore createManagedObjectContexts];
objectManager.managedObjectStore = managedObjectStore;
RKEntityMapping *objMap = [RKEntityMapping mappingForEntityForName:objName inManagedObjectStore:managedObjectStore];
[objMap addAttributeMappingsFromDictionary: @{
@"CODE": @"code",
@"LABEL": @"label",
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objMap pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager getObjectsAtPath:objName parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"ok");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"ko");
}];
return;
You will guess MContextManager
is my context manager, I use it for all my queries with CoreData.
It's been several hours that I'm trying to map this simple Entity, the error I'm getting is the following:
Cannot create managed object contexts: The persistent store coordinator does not have any persistent stores. This likely means that you forgot to add a persistent store or your attempt to do so failed with an error.
I though I was setting the persistent store on the first lines, wasn't I ?
I have added this for persistent store:
NSURL *appDocs = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *storeURL = [appDocs URLByAppendingPathComponent:@"my-model.sqlite"].absoluteString;
[managedObjectStore addSQLitePersistentStoreAtPath:storeURL fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
With still the same error
Upvotes: 2
Views: 708
Reputation: 2512
I attempted to use my own CoreData configuration with the new version of RestKit and ended up running into several issues with objects being accessed from different contexts and data inconsistency. Here is the code for my RestKit initialization:
NSError * error;
NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CoreData" ofType:@"momd"]];
NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore * managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[managedObjectStore createPersistentStoreCoordinator];
NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentPath = [searchPaths objectAtIndex:0];
NSPersistentStore * persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"%@/CoreData.sqlite", documentPath] fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if(!persistentStore){
NSLog(@"Failed to add persistent store: %@", error);
}
[managedObjectStore createManagedObjectContexts];
[RKManagedObjectStore setDefaultStore:managedObjectStore];
_objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/"]];
_objectManager.managedObjectStore = managedObjectStore;
// Object Mapping Code...
From your context manager, you can access the generate context with [RKObjectManager sharedManager].managedObjectStore.mainQueueManagedObjectContext
That should allow you to still make your CoreData queries with your manager.
Upvotes: 2
Reputation: 3447
You are creating a persistent store coordinator but not a persistent store. You need to run addSQLitePersistentStoreAtPath:fromSeedDatabaseAtPath:withConfiguration:options:error:
to save your data to the disk or addInMemoryPersistentStore:
to keep it in memory.
Upvotes: 1