RestKit mapping to a property only - ios

I have a JSON response as follows:
{
response : {
data : {
subjects : [
{
},
{
}
]
}
}
}
and i have a NSManagedObject as follows:
#interface Department : NSManagedObject
#property (nonatomic, retain) NSNumber * id;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSSet *subjects;
#end
I have a department record in Department table with id = 1 and name = "Physics". I want to modify this existing department with obtained subjects from response JSON data. the subjects field only to be updated rest should be kept same. Would you please tell me the RestKit 0.20 mapping code for this?
UPDATE
this is the mapping i performed:
NSDictionary *subMappingDict = #{
#"id": #"id",
#"sub_name": #"name"
};
RKEntityMapping *subMapping = [RKEntityMapping mappingForEntityForName:#"Subject" inManagedObjectStore:managedObjectStore];
[subMapping addAttributeMappingsFromDictionary:subMappingDict];
subMapping.identificationAttributes = #[#"id"];
RKEntityMapping *deptMapping = [RKEntityMapping mappingForEntityForName:#"Department" inManagedObjectStore:managedObjectStore];
[deptMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"subjects" toKeyPath:#"subjects" withMapping:subMapping]];
RKEntityMapping *collegeMapping = [RKEntityMapping mappingForEntityForName:#"College" inManagedObjectStore:managedObjectStore];
[collegeMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"data" toKeyPath:#"department" withMapping:deptMapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:collegeMapping pathPattern:nil keyPath:#"response" statusCodes:statusCodes];
I have an existing College record which has an existing Department record. Here i want to update Department's subjects field only by the mapping.

Related

RestKit using RKConnectionDescription to sideload data [duplicate]

I'm totally new to RestKit and am struggling somewhat.
JSON:
{
"teams": [
{
"id": 1,
"name": "Team A"
},
{
"id": 2,
"name": "Team B"
}
],
"users": [
{
"id": 1,
"name": "cameron",
"teamId": 1
},
{
"id": 2,
"name": "paul",
"teamId": 2
}
]
}
CoreData:
#interface Team : NSManagedObject
#property (nonatomic, retain) NSNumber * teamId;
#property (nonatomic, retain) NSString * name;
#end
#interface User : NSManagedObject
#property (nonatomic, retain) NSNumber * userId;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) Team * team;
#end
My application logic looks like this:
// team mapping
RKEntityMapping *teamMapping = [RKEntityMapping mappingForEntityForName:#"Team" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
teamMapping.identificationAttributes = #[#"teamId"];
[teamMapping addAttributeMappingsFromDictionary:#{
#"id": #"teamId",
#"name": #"name"
}];
// Register our mappings with the provider
RKResponseDescriptor *teamResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:teamMapping
pathPattern:nil
keyPath:#"teams"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:teamResponseDescriptor];
// user mapping
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:#"User" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
userMapping.identificationAttributes = #[#"userId"];
[userMapping addAttributeMappingsFromDictionary:#{
#"id": #"userId",
#"name": #"name"
}];
// Register our mappings with the provider
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
pathPattern:nil
keyPath:#"users"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:userResponseDescriptor];
I can't for the life of me work out how to get RestKit to populate the team Property of the user objects.
I've look at so many posts but nothing I try works, is this not a usual use case?
Does anyone know how to do this, help would be very appreciated!
Thanks.
You need to add a transient attribute to your User entity which holds the associated teamId. This needs to be added to your userMapping.
Then, you need to add a relationship definition to your userMapping:
[userMapping addConnectionForRelationship:#"team" connectedBy:#{ #"teamId": #"teamId" }];
This gives RestKit the information it needs to make the connection and instructs it to make the connection as part of the mapping operation.

RestKit RKObjectMapping of a list

I am new to RestKit and am trying to map a json object that contains an array of objects my model. I have debugged and found that the response hits my server --> json is return --> RestKit says the mapping was successful and that I have 1 object mapped... However, the errorCode field and the array of businesses (bList) are both null when I do BusinessObjectModel *response = result.array.firstObject;
in the OnSuccessBlock.
Json:
{
"bList":
[
{
"id": 1,
"name": "aName",
"owner": 1,
"category": 1,
}
{
"id": 2,
"name": "aName2",
"owner": 1,
"category": 1,
}
],
"errorCode": 0
}
Want to map this Json to this objective c object:
BussinessObjectModel Object Mapping:
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[BusinessObjectModel class]];
[responseMapping addAttributeMappingsFromDictionary:#{
#"id": #"business_id",
#"name": #"business_name",
}];
return responseMapping;
BModel:
#interface bModel : NSObject
#property (nonatomic, copy) NSNumber *errorCode;
#property (nonatomic, copy) NSMutableArray *bList;
+(RKObjectMapping *) getMapping;
#end
BModel Object Mapping:
RKObjectMapping *bMapping = [BusinessObjectModel getMapping];
RKObjectMapping *buslstMapping = [RKObjectMapping mappingForClass:[BModel class]];
[buslstMapping addAttributeMappingsFromDictionary:#{#"errorCode": #"errorCode"}];
// Define the relationship mapping
[buslstMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil
toKeyPath:nil
withMapping:bussinessMapping]];
return buslstMapping;
Descriptor looks as follows:
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:ResponseMapping method:RKRequestMethodAny pathPattern:nil keyPath:#"bList" statusCodes:nil];
EDIT
I want to map the above Json to :
#interface bModel : NSObject
#property (nonatomic, copy) NSNumber *errorCode;
#property (nonatomic, copy) NSMutableArray *bList;
#end
Where bList is an array of the following object:
#interface Business : NSObject
#property (nonatomic, copy) NSNumber *id;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSNumber *id;
#property (nonatomic, copy) NSNumber *id;
#end
I guess the question is how do I do nested relationships (what would the Response Descriptor have to be for the above relationship)?
Great to see that your using RestKit. Your first helper will be the logging that RestKit provides. Add this line of code do your AppDelegate and watch the console for errors.
// The * will send everything RestKit does to the console
// Replace the * with the module you want to check (Network/CoreData/...)
RKLogConfigureByName("RestKit/*", RKLogLevelTrace);
When looking at your JSON you want to get the objects from the keyPath "bList" on the one hand and on the other hand the error code or message when something goes wrong. RestKit provides a build in error handling to get that information out of your JSON.
Init the RKObjectMapping for the RKErrorMessage class and add a RKResponseDescriptor to your requests with the right keyPaths including the range of status codes (here using client errors). RestKit will automatically detect the error code (when sent within the header) and apply the mapping to get the content of the error message.
// Init error mapping
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:#"errorMessage"]];
// Add mapping as response descriptor
RKResponseDescriptor *errorDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:errorMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:#"message" // Edit the keyPath to the value of your JSON (e.g. errorCode)
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[RKObjectManager.sharedManager addResponseDescriptor:errorDescriptor];
To get the error message when a failure occurs, you simply get the message object. When using a block to get objects, you can use the userInfo dictionary from the given NSError.
RKErrorMessage *errorMessage = [[error.userInfo objectForKey:RKObjectMapperErrorObjectsKey] firstObject];
NSLog(#"Error: %#", errorMessage);
Now you can simplify your object model a bit and concentrate on mapping the BusinessObjectModel. When mapping the object using the dictionary, you need to check of your local attributes matches the value in your JSON.
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[BusinessObjectModel class]];
[responseMapping addAttributeMappingsFromDictionary:#
{
#"value_from_remote_json": #"value_in_local_object", // e.g. #"id" : #"business_id"
...
}];
return responseMapping;
You don't need to use a RKRelationshipMapping any more. Reconfigure your objects/mappings and try again. The logging will show you the provided mapping and if the mapping operations are working. Last bit not least make sure that the mapping is in memory when using it by throwing an NSAssert error.
RKObjectMapping *bMapping = [BusinessObjectModel getMapping];
NSAssert(bMapping, #"bMapping mapping must not be nil");
Edit
To map values without a keyPath (like the "errorCode" field) additionally to the mapping of your objects, you'll need to provide an object with an according mapping. Taking the example from the documentation you'll end with something like:
// Init object
#interface RKErrorCode : NSObject
#property (nonatomic) NSNumber *errorCode;
#end
// Init mapping
RKObjectMapping *codeMapping = [RKObjectMapping mappingForClass:[RKErrorCode class]];
[codeMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:#"errorCode"]];
// Add response descriptor for request
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:codeMapping method:RKRequestMethodAny pathPattern:nil keyPath:#"errorCode" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
Your response descriptor shouldn't have keyPath:#"bList", it should be set to nil as you don't want to drill in.
It also shouldn't use ResponseMapping based on the structure of your mappings, it should use the other mapping.
Your mapping is also wrong here:
#{
#"business_id": #"business_id",
#"business_name": #"business_name",
}];
It should be:
#{
#"id": #"business_id",
#"name": #"business_name",
}];
Because this specifies the JSON names and the core data names.
To fix your new issues, put this back:
[buslstMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"bList" toKeyPath:#"bList"
And this:
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:buslstMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Found the solution and it looks something like this:
RKObjectMapping *bModelMapping = [RKObjectMapping mappingForClass:[business class]];
[bModelMapping addAttributeMappingsFromDictionary:#{
#"id": #"business_id",
#"name": #"business_name",
}];
RKObjectMapping *buslstMapping = [RKObjectMapping mappingForClass:[bModel class]];
[buslstMapping addAttributeMappingsFromDictionary:#{#"errorCode": #"errorCode"}];
// Define the relationship mapping
[buslstMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"bList"
toKeyPath:#"bList" bModelMapping]];
return buslstMapping;
Also Change the following:
#property (nonatomic, copy) NSMutableArray *bList;
to
#property (nonatomic, retain) NSArray *bList;
This gives me a bModel object with an error code value and a bList which is an array of business which can then be cast to a business by doing the following Business *business = [bModel.blist firstObject];

Restkit mapping objects to NSArray

I have an object that consists of some fields such as:
#property (nonatomic, copy) NSString* title;
#property (nonatomic, copy) NSString* body;
#property (nonatomic, copy) NSArray* imageUrls;
#property (nonatomic, copy) NSString* postId;
#property (nonatomic) CLLocationCoordinate2D location;
#property (nonatomic) LTUser *user;
#property (nonatomic) LTPlace *place;
#property (nonatomic, copy) NSArray* comments;
All NSString and custom objects (such as LTUser/LTPlace) and it is mapping well.
But, how can I map to the NSArray of (imageUrls - which is an array of NSString / comments - which is an array of custom objects (LTComment))?
"images": [
"http://****.com/images/1385929903887.jpg",
"http://****.com/images/131315313131.jpg",
"http://****.com/images/1351351351.jpg"
]
Mapping for main object:
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[LTUser class]];
[userMapping addAttributeMappingsFromDictionary:#{
#"_id":#"userId",
#"username":#"userName",
#"name":#"name"
}];
RKObjectMapping *placeMapping = [RKObjectMapping mappingForClass:[LTPlace class]];
[placeMapping addAttributeMappingsFromDictionary:#{
#"_id":#"placeId",
#"image":#"name",
#"name":#"image"
}];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[LTPost class]];
[mapping addAttributeMappingsFromDictionary:#{
#"_id" : #"postId",
#"createdAt" : #"createdAt",
#"body" : #"body",
#"title" : #"title"
}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"user" toKeyPath:#"user" withMapping:userMapping]];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"place" toKeyPath:#"place" withMapping:placeMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodGET
pathPattern:kLTAPIGetPostsRequest
keyPath:#"posts"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
LTComment mapping
RKObjectMapping* commentMapping = [RKObjectMapping mappingForClass:[LTComment class]];
[commentMapping addAttributeMappingsFromDictionary:#{
#"user_name":#"userName",
#"text":#"text"
}];
The mapping for comments should be just like you mappings for user and place (just with a different mapping obviously, commentMapping). RestKit will determine that the destination is a collection and that the source is a collection and do the right thing.
For imageUrls, the JSON is already an array of strings so RestKit can basically copy it. All you need to do is add to the 'container' mapping (whichever that one is):
#"images" : #"imageUrls"

Foreign key relationship mapping with RestKit

I'm totally new to RestKit and am struggling somewhat.
JSON:
{
"teams": [
{
"id": 1,
"name": "Team A"
},
{
"id": 2,
"name": "Team B"
}
],
"users": [
{
"id": 1,
"name": "cameron",
"teamId": 1
},
{
"id": 2,
"name": "paul",
"teamId": 2
}
]
}
CoreData:
#interface Team : NSManagedObject
#property (nonatomic, retain) NSNumber * teamId;
#property (nonatomic, retain) NSString * name;
#end
#interface User : NSManagedObject
#property (nonatomic, retain) NSNumber * userId;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) Team * team;
#end
My application logic looks like this:
// team mapping
RKEntityMapping *teamMapping = [RKEntityMapping mappingForEntityForName:#"Team" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
teamMapping.identificationAttributes = #[#"teamId"];
[teamMapping addAttributeMappingsFromDictionary:#{
#"id": #"teamId",
#"name": #"name"
}];
// Register our mappings with the provider
RKResponseDescriptor *teamResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:teamMapping
pathPattern:nil
keyPath:#"teams"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:teamResponseDescriptor];
// user mapping
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:#"User" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
userMapping.identificationAttributes = #[#"userId"];
[userMapping addAttributeMappingsFromDictionary:#{
#"id": #"userId",
#"name": #"name"
}];
// Register our mappings with the provider
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
pathPattern:nil
keyPath:#"users"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:userResponseDescriptor];
I can't for the life of me work out how to get RestKit to populate the team Property of the user objects.
I've look at so many posts but nothing I try works, is this not a usual use case?
Does anyone know how to do this, help would be very appreciated!
Thanks.
You need to add a transient attribute to your User entity which holds the associated teamId. This needs to be added to your userMapping.
Then, you need to add a relationship definition to your userMapping:
[userMapping addConnectionForRelationship:#"team" connectedBy:#{ #"teamId": #"teamId" }];
This gives RestKit the information it needs to make the connection and instructs it to make the connection as part of the mapping operation.

Adding two request descriptors for a given class in Restkit 0.2

I need to make two different types of POST coming from the User class.
//JSON Type A
{
"password":"12345",
"email":"test#gmail.com"
}
//JSON Type B
{
"user":{
"Password":"12345",
"Email":"sample#gmail.com"
}
}
I've tried to make two request descriptors and adding them to my object manager however I get the error
"Cannot add a request descriptor for the same object class as an
existing request descriptor."
My code
#interface User : NSObject
#property (nonatomic, retain) NSString * userID;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSString * password;
#property (nonatomic, retain) NSString * firstName;
#property (nonatomic, retain) NSString * lastName;
#end
- (void)setupUserMapping:(RKObjectManager *)objectManager {
// Setup user response mappings
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping addAttributeMappingsFromDictionary:#{
#"ID" :#"userID",
#"Email" : #"email",
#"Password" : #"password",
#"FirstName" : #"firstName",
#"LastName" : #"lastName",
}];
RKResponseDescriptor *responseDescriptorAuthenticate = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
pathPattern:#"/Authenticate"
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
RKResponseDescriptor *responseDescriptorRegister = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
pathPattern:#"/Register"
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptorRegister];
[objectManager addResponseDescriptor:responseDescriptorAuthenticate];
// Setup user request mappings
RKObjectMapping* userRequestMappingForRegister = [RKObjectMapping requestMapping];
[userRequestMappingForRegister addAttributeMappingsFromDictionary:#{
#"email" : #"Email",
#"password" : #"Password",
#"firstName" : #"FirstName",
#"lastName" : #"LastName",
}];
RKRequestDescriptor *requestDescriptorForRegister = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForRegister objectClass:[User class] rootKeyPath:#"user"];
RKObjectMapping* userRequestMappingForAuthenticate = [RKObjectMapping requestMapping];
[userRequestMappingForAuthenticate addAttributeMappingsFromDictionary:#{
#"userID" :#"ID",
#"email" : #"email",
#"password": #"password"
}];
RKRequestDescriptor *requestDescriptorForAuthenticate = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForAuthenticate objectClass:[User class] rootKeyPath:nil];
[objectManager addRequestDescriptor:requestDescriptorForRegister];
[objectManager addRequestDescriptor:requestDescriptorForAuthenticate];
}
Does anyone know how I can solve this problem without creating a separate class for these requests?
Any help is appreciated.
Thanks.
You can use a dynamic mapping to switch the serialization behaviors. If this is a common enough issue, we could conceivably add path matching to the request descriptor. I just have not had a ton of requests for such a feature.
There is an example of how to use the dynamic mapping with a request in the unit tests: https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534
For multiple request descriptors, I declared a new model class with the same data members as the earlier one, and then referenced it while adding the request descriptor instead of the earlier one as follow.
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[CMAGoogleUserDataModel class]];
Here the newly created class was "CMAGoogleUserDataModel"
Noted: I am not sure whether it is the optimised one or not, but it did solve my use case.

Resources