Is there a way to POST large NSObject-derived object structures without having to manually specify every property and property collection to RestKit?
Here is a simple example, with a single class DABUser
But imagine it contained properties which were also objects, collections, and those had more of the same to represent some larger object tree.
The class to POST:
@interface DABUser : NSObject
@property (nonatomic) int age;
@property (copy, nonatomic) NSString *name;
@end
POST a DABUser
object:
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromArray:@[ @"age", @"name"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[DABUser class] rootKeyPath:nil method:RKRequestMethodPOST];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager addRequestDescriptor:requestDescriptor];
DABUser *user = [[DABUser alloc] init];
user.age = 20;
user.name = @"Charlie Brown";
[objectManager postObject:user path:@"users/123" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Success!");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed!");
}];
The JSON generated from the above code being and sent via the request body is:
{ "age":20,"name":"Charlie Brown" }
When I have a large object tree, defining the property mappings can get tiresome (and error-prone), with many lines of similar code to this example's:
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromArray:@[ @"age", @"name"]];
Is there a way that I could just get RestKit to generate the JSON from the objects, without all this setup?