I have a managed object MyEntity that I use without any problem throughout my application.
#interface MyEntity : NSManagedObject
#property (nonatomic, retain) NSString *code;
#property (nonatomic, retain) NSNumber *def_value;
#property (nonatomic, retain) NSString *label;
#property (nonatomic, retain) NSString *link_label;
#property (nonatomic, retain) NSNumber *order;
#end
I have a web service http://my_server/MyEntity which returns me:
[
{
CODE: "TYPE",
LABEL: "Administrative",
ORDER: "3",
DEF_VALUE: "0",
LINK_LABEL: ""
},
{
CODE: "TOPIC",
LABEL: "NDF",
ORDER: "1",
DEF_VALUE: "0",
LINK_LABEL: "Administrative"
},
{
CODE: "TOPIC",
LABEL: "Report",
ORDER: "2",
DEF_VALUE: "1",
LINK_LABEL: "Administrative"
},
{
CODE: "TOPIC",
LABEL: "Other",
ORDER: "3",
DEF_VALUE: "0",
LINK_LABEL: "Administrative"
}, ...
]
I'm trying to map and fetch this list
NSURL *baseUrl = [NSURL URLWithString: #"http://my_server"];
NSString *objName = #"MyEntity";
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[RKManagedObjectStore setDefaultStore:managedObjectStore];
[managedObjectStore createPersistentStoreCoordinator];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL: baseUrl];
RKEntityMapping *objMap = [RKEntityMapping mappingForEntityForName:objName inManagedObjectStore:managedObjectStore];
[objMap addAttributeMappingsFromDictionary: #{
#"CODE": #"code",
#"LABEL": #"label",
#"ORDER": #"order",
#"DEF_VALUE": #"def_value",
#"LINK_LABEL": #"link_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");
}];
It's giving me this error
I restkit.network:RKHTTPRequestOperation.m:185 GET'http://my_server/MyEntity'(200 OK)
CoreData: error: Failed to call designated initializer on NSManagedObject class 'MyEntity'
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "link_label".'
I saw this error on 2 or 3 SO questions but each with a different problem and seem not any of mine.
Can someone help me to debug that ?
You need to fully configure the objectManager:
objectManager.managedObjectStore = managedObjectStore;
You also aren't completing the Core Data stack initialisation:
[managedObjectStore createManagedObjectContexts];
Related
I have NSDictionary object as result of NSJSONSerialization, and it has child that is array of dictionary called list. Look at my json result:
{
"tags": [
"rofl",
"lmao",
"funny",
"haha",
"lmfao"
],
"result_type": "exact",
"list": [
{
"defid": 3689813,
"word": "Lol",
"author": "Lol B",
"permalink": "http://lol.urbanup.com/3689813",
"definition": "The name 'Lol' is an abreviated form of the name '[Laurence]'.",
"example": "\"Hey Lol, you alright?\"\r\n\r\n\"..Well i was chattin to Lol and he said..\"",
"thumbs_up": 44617,
"thumbs_down": 9926,
"current_vote": ""
},
------- bla bla bla ----------
],
"sounds": [
"http://media.urbandictionary.com/sound/lol-871.mp3",
------- bla bla bla ----------
]
}
I also created a class model for item (child of list).
#import <Foundation/Foundation.h>
#interface Item : NSObject
#property (strong, nonatomic) NSString *defid;
#property (strong, nonatomic) NSString *word;
#property (strong, nonatomic) NSString *author;
#property (strong, nonatomic) NSURL *permalink;
#property (strong, nonatomic) NSString *definition;
#property (strong, nonatomic) NSString *example;
-(instancetype)initWithDictionary:(NSDictionary *)dict;
#end
I also created its initializer
-(instancetype)initWithDictionary:(NSDictionary *)dict {
if (self = [super init]) {
self.defid = [dict valueForKey:#"defid"];
self.word = [dict valueForKey:#"word"];
self.author = [dict valueForKey:#"author"];
self.permalink = [dict valueForKey:#"permalink"];
self.definition = [dict valueForKey:#"definition"];
self.example = [dict valueForKey:#"example"];
}
return self;
}
I already create an array of list item (NSDictionary), my problem is I want to map those into my class so I can create an instance of list item. So how to convert item in array into my item class?
Should I iterate each item of array and call that initialization?
or any elegant method for doing that?
Additional question
I am relatively new into iOS development. I just want to parse my request result. Is there any tips for fetch some request and assign its result for datasource a table view (array)?
This is my fetch method:
- (void)fetchDataFromMashape:(NSURL *)URL {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:#"GET"];
[request setValue:API_KEY_MASHAPE forHTTPHeaderField:API_MASHAPE_HEADER_1];
[request setValue:API_ACCEPT forHTTPHeaderField:API_MASHAPE_HEADER_2];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"Result: %#", jsonResult);
_results = [[NSArray alloc] initWithArray:[jsonResult valueForKey:#"list"]];
}];
[task resume];
}
My advice is to create a static method with the json list as a parameter
+ (NSArray<Item*>*) parseJson:(NSArray*) json {
NSMutableArray<Item*>* results = [[NSMutableArray alloc] initWithCapacity:son.length];
for (NSDictionary* anItem in json){
[results append:[[Item alloc] initWithDictionary:anItem]];
}
return results;
}
I am having trouble with RESTKIT and mapping the following JSON via RESTKit .20.1. I have a nested JSON array and I am trying to map via the RelationshipMapper. I am able to map the 5 chart_data object successfully but the related nested chart_datatum objects do not map to the ChartDataDetails class. All of the properties are blank.
I would like the following object structure.
Alert Instance
Array of ChartData objects
Each ChartData object should contain 1 ChartDataDetails object <---- this object is blank
Please see the link at the bottom for the screenshot of the XCode Debugger for the current state.
Can someone please provide some assistance?
Thanks,
G
{
"alert_instance": {
"alert_template_id": 1,
"alert_time_period": "2013-05-29",
"chart_data": [
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "provider_id",
"datatype": 1,
"chart_data_value": "1"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Provider_name",
"datatype": 2,
"chart_data_value": "Stubbs, Drew"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Posting_date",
"datatype": 3,
"chart_data_value": "05/12/2013"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Charges",
"datatype": 5,
"chart_data_value": "229"
}
},
{
"chart_datum": {
"alert_instance_id": 1,
"chart_data_id": 1,
"chart_data_name": "Payments",
"datatype": 5,
"chart_data_value": "-1023.11"
}
}
]
}
Model classes
AlertInstance.h
#interface AlertInstance : NSObject
#property (nonatomic, copy) NSNumber* alertTemplateId;
#property (nonatomic, copy) NSDate* alertTimePeriod;
#property (nonatomic, retain) NSMutableArray* chartData;
#end
ChartData.h
#interface ChartData : NSObject
#property (nonatomic, retain) ChartDataDetails *chartDataDetails;
#end
ChartDataDetails.h
#interface ChartDataDetails : NSObject
#property (nonatomic, copy) NSNumber* chartDataId;
#property (nonatomic, copy) NSString* chartDataName;
#property (nonatomic, copy) NSNumber* chartDataType;
#property (nonatomic, copy) NSNumber* chartDataValue;
#end
code for mappings..
RKObjectMapping* chartDataDetailMapping = [RKObjectMapping mappingForClass:[ChartDataDetails class]];
[chartDataDetailMapping addAttributeMappingsFromDictionary:#{
#"chart_data_id": #"chartDataId",
#"chart_data_name": #"chartDataName",
#"datatype": #"chartDataType",
#"chart_data_value": #"chartDataValue",
}];
RKObjectMapping* chartDataMapping = [RKObjectMapping mappingForClass:[ChartData class]];
[chartDataMapping addAttributeMappingsFromDictionary:#{
#"chart_data":#"chartData"}];
RKObjectMapping* alertInstanceMapping = [RKObjectMapping mappingForClass:[AlertInstance class]];
[alertInstanceMapping addAttributeMappingsFromDictionary:#{
#"alert_template_id": #"alertTemplateId",
#"alert_time_period": #"alertTimePeriod",
}];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"chart_data"
toKeyPath:#"chartData"
withMapping:chartDataMapping]];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"chart_datum"
toKeyPath:#"chartDataDetails"
withMapping:chartDataDetailMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:alertInstanceMapping
pathPattern:nil
keyPath:#"alert_instance"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:#"http://ideainnovationsmobiledev1.cloudapp.net:3000/alert_instances/1.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:#[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
link to screen shot of the debugger
https://docs.google.com/file/d/0Bwaj6O1rXiKaeFIwT2s4Rm1Gb00/edit?usp=sharing
EDIT:
Ok I figured it out.
I needed to chain the ChartDetailsMapping to the ChartData as follows.
[chartDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"chart_datum"
toKeyPath:#"chartDataDetails"
withMapping:chartDataDetailMapping]];
[alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"chart_data"
toKeyPath:#"chartData"
withMapping:chartDataMapping]];
I am trying to bind my core data to a NSTableView. I am getting information from an API then wanting to add it to NSTableView. It looks like it is setup correctly because each time I have it call the API and get information back, a blank line is added to the NSTableView data.
Why is it adding a blank line instead of the data I have it binded too?
AppController.h
#property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
I then am using the new Xcode where it auto synth's.
Items.h
#class TimeLog;
#interface Items : NSManagedObject
#property (nonatomic, retain) NSNumber * itemId;
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSString * itemType;
#property (nonatomic, retain) TimeLog *relationship;
#end
Items.m
#implementation Items
#dynamic itemId;
#dynamic title;
#dynamic itemType;
#dynamic relationship;
#end
ItemObject.h
#interface ItemObject : NSObject
#property (nonatomic, retain) NSString * itemId;
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSString * itemType;
#end
ItemObject.m
#implementation ItemObject
#end
Method making API Call
This method makes the API call and adds it to a temp object. It then adds that temp object to core data.
+ (void)searchForItemByType:(NSString *)itemType andId:(NSString *)searchId
{
NSLog(#"Search Feature By ID: %#", searchId);
RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[ItemObject class]];
[itemMapping addAttributeMappingsFromDictionary:#{
#"id": #"itemId",
#"name": #"title",
#"item_type": #"itemType"
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:itemMapping pathPattern:nil keyPath:#"data" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
// The entire value at the source key path containing the errors maps to the message
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:#"errorMessage"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError);
// Any response in the 4xx status code range with an "errors" key path uses this mapping
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:#"error_description" statusCodes:statusCodes];
RKObjectManager *manager = [RKObjectManager sharedManager];
NSLog(#"HTTP Client: %#", manager.HTTPClient);
[manager addResponseDescriptorsFromArray:#[ responseDescriptor, errorDescriptor ]];
// NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"false", #"with_lock"
, nil];
NSString *path = [NSString stringWithFormat:#"/api/v1/%#/%#", [itemType lowercaseString], searchId];
NSLog(#"Manager: %#", manager);
[manager getObjectsAtPath:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(#"Results: %#", [result firstObject]);
Items *insertItem = [NSEntityDescription insertNewObjectForEntityForName:#"Items" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
insertItem = [result firstObject];
NSLog(#"Name: %#", [insertItem title]);
// Handled with articleDescriptor
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// Transport error or server error handled by errorDescriptor
NSLog(#"Error: %#", [error localizedDescription]);
NSAlert *alert = [NSAlert alertWithMessageText:#"Error" defaultButton:#"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:#"%#", [error localizedDescription]];
[alert runModal];
}];
}
Log from above code
2013-03-15 10:15:21.817 Project[59074:403] Results: <ItemObject: 0x1034ab360>
2013-03-15 10:15:21.818 Project[59074:403] ManagedObjectContext
2013-03-15 10:15:21.818 Project[59074:403] Name: Custom Mod is missing from Face Lift
IB
I think the problem may be in this code:
NSLog(#"Results: %#", [result firstObject]);
Items *insertItem = [NSEntityDescription insertNewObjectForEntityForName:#"Items" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
insertItem = [result firstObject];
NSLog(#"Name: %#", [insertItem title]);
In your log it looks like [result firstObject] is part of the 'ItemObject' class not the 'Items' class. Even though they share the same structure, 'ItemObject' does not inherit from NSManagedObject, but is being assigned to one. The system doesn't know how to translate an 'ItemObject' object into an 'Items' object so it simply keeps all the values in insertItem blank, which translates into a blank line showing up in your table. Try this instead:
Items *insertItem = [NSEntityDescription insertNewObjectForEntityForName:#"Items" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
ItemObject *tempObject = [result firstObject];
insertItem.itemID = tempObject.itemID;
insertItem.title = tempObject.title;
insertItem.itemType = tempObject.itemType;
Suppose I want to send a request to the server with the json
{ "begin_session" : { "info" : "this is some info" } }
and I expect in response the json:
{ "token" : "this is a token", "a_objects" : [
{ "name" : "name of first a_object", "b_objects" : [
{ "name" : "name of first b_object", "type" : "some type value", "id" : "123" },
{ "name" : "name of second b_object", "type" : "some other type value", "id" : "124" }
], "id" : "id of first a_object" },
{ "name" : "name of second a_object", "b_objects" : [
{ "name" : "name of first b_object", "type" : "some type value", "id" : "123" },
{ "name" : "name of third b_object", "type" : "some third type value" , "id" : "125" },
], "id" : "id of second a_object" }
] }
I want to store "token" transiently and persist the a_objects in core data. Is this how I should do the entire process? First, I set up the objects:
#interface LoginToken : NSObject
#property (nonatomic, copy) NSString *token;
#end
#interface AObject : NSManagedObject
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSSet *bObjects;
#property (nonatomic, retain) NSString *aObjectId;
#end
#implementation AObject
#dynamic name; #dynamic bObjects; #dynamic aObjectId;
#end
#interface BObject : NSManagedObject
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) AObject *aObject;
#property (nonatomic, retain) NSString *type;
#property (nonatomic, retain) NSString *bObjectId;
#end
#implementation BObject
#dynamic name; #dynamic aObject; #dynamic type; #dynamic bObjectId;
#end
These are the request parameters:
NSDictionary *params = #{"begin_session":#{#"info":#"this is some info"}};
Then I set up the mappings:
RKObjectMapping *tokenMapping = [RKObjectMapping mappingForClass:[LoginToken class]];
[tokenMapping addAttributeMappingsFromArray:#[#"token"]];
RKResponseDescriptor *tokenResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:tokenMapping pathPattern:nil keyPath:#"token" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKEntityMapping *bObjectMapping = [RKEntityMapping mappingForEntityForName:#"BObject" inManagedObjectStore:objectManager.managedObjectStore];
[bObjectMapping addAttributeMappingsFromDictionary:#{#"name":#"name",#"type":#"type", #"id":#"bObjectId"}];
bObjectMapping.identificationAttributes = #[#"bObjectId"];
RKEntityMapping *aObjectMapping = [RKEntityMapping mappingForEntityForName:#"AObject" inManagedObjectStore:objectManager.managedObjectStore];
[aObjectMapping addAttributeMappingsFromDictionary:#{#"name":#"name",#"id":#"aObjectId"}];
[aObjectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"b_objects" toKeyPath:#"bObjects" withMapping:bObjectMapping]];
aObjectMapping.identificationAttributes = #[#"aObjectId"];
Suppose objectManager is a correctly configured RKObjectManager. I set up the response descriptors:
RKResponseDescriptor *tokenResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:tokenMapping pathPattern:nil keyPath:#"token" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKResponseDescriptor *aObjectResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:aObjectMapping pathPattern:nil keyPath:#"a_objects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:#[tokenResponseDescriptor, aObjectResponseDescriptor]];
And finally I'll make the request:
[objectManager getObjectsAtPath:#"path" parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
LoginToken *token = [mappingResult firstObject]; // use this token transiently
// coredata objects are auto saved
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// handle error
}];
Is there anything I need to be aware of if this is, in fact, the correct way to do it? Also, how do I set the inverse relationship from BObject to AObject...?
As long as your CoreData file has the relationship configured properly (which it looks correct based on your object header files) then the inverse relationship is handled by the mapper. Otherwise, it should work as is.
Note that the token object will be persisted in CoreData as well so you might have to delete them that's the desired behaviour.
Here is my setup, Post a list of objects in Json to server and got back with updated results that will be mapped to core data and updated.
-- Post Boday --
{
"memberId": "1000000",
"contacts": [
{
"phoneNumber": "+12233333333",
"firstName": "john",
"lastName": "H"
},
{
"phoneNumber": "+12244444444",
"firstName": "mary",
"lastName": "K"
}
]
}
-- Post Response --
{
"contacts": [
{
"phoneNumber": "+12233333333",
"firstName": "john",
"lastName": "k",
"isMember": "yes"
},
{
"phoneNumber": "+12244444444",
"firstName": "mary",
"lastName": "k",
"isMember": "no"
}
]
}
I found another thread which discusses very similar to my case. RestKit: How does one post an array of objects?
This is my setup.
-- SHContact.h --
#interface SHContact : NSManagedObject {}
#property (nonatomic, strong) NSString* phoneNumber;
#property (nonatomic, strong) NSString* firstName;
#property (nonatomic, strong) NSString* lastName;
#property (nonatomic, strong) NSNumber* isMember;
#end
-- SHContactPost.m --
#import "SHContactPost.h"
#implementation SHContactPost
#synthesize contacts;
#synthesize memberId;
- (NSArray*)contacts {
return <list-of-SHContact>;
}
- (NSString *)memberId {
return #"my-member-id";
}
#end
-- RK Mapping --
// Setup our object mappings for SHContact
RKManagedObjectMapping* contactMapping =
[RKManagedObjectMapping mappingForClass:[SHContact class]
inManagedObjectStore:objectManager.objectStore];
contactMapping.primaryKeyAttribute = #"phoneNumber";
[contactMapping mapAttributes:#"phoneNumber",
#"firstName", #"lastName", #"isMember", nil];
[objectManager.mappingProvider setObjectMapping:contactMapping
forKeyPath:#"contacts"];
[[objectManager mappingProvider]
setSerializationMapping:[contactMapping inverseMapping]
forClass:[SHContact class]];
RKObjectMapping *cPostMapping = [RKObjectMapping
mappingForClass:[SHContactPost class]];
[cPostMapping mapKeyPath:#"contacts"
toRelationship:#"contacts"
withMapping:contactMapping];
[cPostMapping mapAttributes:#"memberId", nil];
[objectManager.mappingProvider
setSerializationMapping:[cPostMapping inverseMapping]
forClass:[SHContactPost class]];
[objectManager.router routeClass:[SHContactPost class]
toResourcePath:#"/contacts"
forMethod:RKRequestMethodPOST];
objectManager.serializationMIMEType = RKMIMETypeJSON;
-- Post to server --
SHContactPost *post = [[SHContactPost alloc] init];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager postObject:post delegate:self];
Server post and response is SUCCESSFUL. However, Reskit was not able to map the result back to the list of SHContact objects. This is the exception. Am I missing something?
restkit.network:RKObjectLoader.m:222 Encountered errors during mapping:
Cannot map a collection of objects onto a non-mutable collection.
Unexpected destination object type 'SHContactPost'
UPDATE: This is what I changed to make it work for me.
-- RK Mapping --
// Setup our object mappings for SHContact
RKManagedObjectMapping* contactMapping =
[RKManagedObjectMapping mappingForClass:[SHContact class]
inManagedObjectStore:objectManager.objectStore];
contactMapping.primaryKeyAttribute = #"phoneNumber";
[contactMapping mapAttributes:#"phoneNumber",
#"firstName", #"lastName", #"isMember", nil];
[objectManager.mappingProvider addObjectMapping:contactMapping];
-- Post to server --
SHContactPost *post = [[SHContactPost alloc] init];
RKObjectManager* manager = [RKObjectManager sharedManager];
RKObjectMapping* responseMapping = [manager.mappingProvider
objectMappingForClass:[SHContact class]];
[manager postObject:post usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
loader.targetObject = nil;
responseMapping.rootKeyPath = #"contacts";
loader.objectMapping = responseMapping;
}];
loader.targetObject needs to set it to nil, otherwise, it won't work.