RestKit : Not able to perform mapping using coredata

Posted by Ashish Beuwria on Stack Overflow See other posts from Stack Overflow or by Ashish Beuwria
Published on 2013-11-08T13:13:10Z Indexed on 2013/11/08 15:54 UTC
Read the original article Hit count: 407

Filed under:
|
|
|

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)'

© Stack Overflow or respective owner

Related posts about ios

Related posts about core-data