Nicholas Curtis
Nicholas Curtis

Reputation: 43

RestKit Object Mapping from JSON

I am trying to create a mapping for the following JSON data.

{
  "Calls": [
    {
      "id": "18",
      "parent_id": "0",
      "status": "completed",
    },
    {
      "id": "19",
      "parent_id": "0",
      "status": "completed",
    },
}

I am using the following methods to map and handle the response from the delegate. When I run this the didLoadObjects method is never hit and nothing is logged. Also, I am positive that the URL is correct and the URL returns the JSON i posted above.

- (void)viewDidLoad
{
    [super viewDidLoad];

    RKLogConfigureByName("RestKit/*", RKLogLevelTrace);

    RKObjectMapping *callMapping = [RKObjectMapping mappingForClass:[YCMDCall class]];

    [callMapping mapKeyPath:@"id" toAttribute:@"call_id"];
    [callMapping mapKeyPath:@"parent_id" toAttribute:@"parent_id"];
    [callMapping mapKeyPath:@"status" toAttribute:@"status"];

    RKObjectManager *manager = [[RKObjectManager sharedManager] managerWithBaseURL:@"http://MYBASEURL/"];
    [manager.mappingProvider setMapping:callMapping forKeyPath:@"Calls"];
    [manager loadObjectsAtResourcePath:@"RESTOFURL" objectMapping:callMapping delegate:self];
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
{
    NSLog(@"Load collection of Articles: %@", objects);
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
    NSLog(@"Oh no! Error: %@", [error localizedDescription]);
}

Also, if anyone could provide an example or point me in the right direction to import these results into a TableViewController. Thanks in advance for any help.

@interface YCMDCall : NSObject

@property (nonatomic, retain) NSString* call_id;
@property (nonatomic, retain) NSNumber* parent_id;
@property (nonatomic, retain) NSString* status;

@end



@implementation YCMDCall

@synthesize call_id;
@synthesize parent_id;
@synthesize status;

@end

Upvotes: 1

Views: 759

Answers (1)

Beber
Beber

Reputation: 1513

You can save reterned NSArray in a Session Object who is a singleton, and then post a notification :

[MySessionObject getInstance].list = objects;
[[NSNotificationCenter defaultCenter] postNotificationName:@"YCMDCallSuccess" object: nil];

In your view, you can observe this notification :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onYCMDCallSuccess) name:@"YCMDCallSuccess" object:nil];

In your onYCMDCallSuccess method you can access data in your Session Object

// For exemple
self.myList=[MySessionObject getInstance].list;

You can see this post for more details : RestKit 0.9.3 app Structure

Hope it can help you.

Edit : If your call is directly in your view controller, see if you added RKObjectLoaderDelegate

 @interface MyTableViewController : UITableViewController <RKObjectLoaderDelegate>

if not, Your callback functions will never be called :

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error;

Upvotes: 2

Related Questions