3

I'm using rest kit 0.20.3 and Xcode 5. Without core data I'm able to perform all rest kit operation, but when I've tried it with core data, I'm not even able to perform GET due to some problem. I can't figure it out. I'm new with core data. So pls help. Here is my code:

AppDelegate.m

@implementation CardGameAppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RKLogConfigureByName("RestKit", RKLogLevelWarning);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.3:3010/"]];


    RKManagedObjectStore *objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.managedObjectModel];

    objectManager.managedObjectStore = objectStore;

    RKEntityMapping *playerMapping = [RKEntityMapping mappingForEntityForName:@"Player" inManagedObjectStore:objectStore];
    [playerMapping addAttributeMappingsFromDictionary:@{@"id": @"playerId",
                                                        @"name": @"playerName",
                                                        @"age" : @"playerAge",
                                                        @"created_at": @"createdAt",
                                                        @"updated_at": @"updatedAt"}];



    RKResponseDescriptor *responseDesc = [RKResponseDescriptor responseDescriptorWithMapping:playerMapping method:RKRequestMethodGET pathPattern:@"/players.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [objectManager addResponseDescriptor:responseDesc];

    PlayersTableViewController *ptvc = (PlayersTableViewController *)self.window.rootViewController;
    ptvc.managedObjectContext = self.managedObjectContext;

    return YES;
}

and code for playerTableViewController.h

#import <UIKit/UIKit.h>

@interface PlayersTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@end

and PlayerTableViewController.m get method:

-(void)loadPlayers{
    [[RKObjectManager sharedManager] getObjectsAtPath:@"/players.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
        [self.refreshControl endRefreshing];
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        [self.refreshControl endRefreshing];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }];
}

I'm getting the following error :

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to perform mapping: No `managedObjectContext` assigned. (Mapping response.URL = http://192.168.1.3:3010/players.json)'
Wain
  • 118,658
  • 15
  • 128
  • 151
Ashish Beuwria
  • 975
  • 6
  • 13
  • 17

1 Answers1

3

It isn't enough to just create the objectStore, you need to complete the rest of the Core Data stack setup. You should also do this in RestKit, not in the app delegate (which is the default Apple provided configuration). This will be something along the lines of (tailor for your requirements):

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

[managedObjectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"XXXX.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);

// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];

managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • where should i put this code if not in the app delegate. Sorry for these type of questions, but i'm new with this stuff. Thank You – Ashish Beuwria Nov 08 '13 at 16:20
  • No, this code can go in the app delegate. I mean that the app delegate should not explicitly be creating the `managedObjectContext`, `managedObjectModel`, `persistentStoreCoordinator` (which I guess it currently is due to your `@synthesize` statements. – Wain Nov 08 '13 at 16:45
  • I've completely wrote my code from start with a Master Detail template using your code...It worked and shows in my log that i've recieved all the objects from my server,i.e. = 11, But the problem is that I can't make it to appear in my tableView. Pls Help – Ashish Beuwria Nov 08 '13 at 18:36
  • If you're using a fetched results controller it should be automatic (the template has observation with the delegate). You should probably start a new question showing your mappings / response descriptor / result of the `mappingResult` is your success block. – Wain Nov 08 '13 at 19:21
  • I've posted my question [Here](http://stackoverflow.com/questions/19874009/restkit-not-been-able-to-display-json-data-onto-my-tableview). Pls help. Thank You – Ashish Beuwria Nov 09 '13 at 08:37