0

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?

Diego Barros
  • 2,071
  • 2
  • 33
  • 45
  • maybe you were trying to serialise NSObject to JSON? http://stackoverflow.com/questions/10515015/ios-json-serialization-for-nsobject-based-classes – Lunayo Jul 25 '13 at 03:02
  • Serialising from the Object to JSON was an option. And thank you for the link, may be useful in future. Although the introduction of a new library and the code around that, I may as well just manually setup the request mappings. – Diego Barros Jul 26 '13 at 05:58

1 Answers1

1

"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:"

I personally think this is the easiest way and a good approach.I have done object mapping to large objects with so many object mapping and multiple object linking and found this is the easiest way to deal with it correctly

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • In the end I went with this route. Took a bit to figure out how to get the large object structure, but seems to be working. I just need to make sure I keep things in sync when fiddling the object structure to make sure the JSON then matches when its spit back out to the server. – Diego Barros Jul 26 '13 at 05:59
  • @dbarros : ?Use this way it is a good approch I have done over 10 level json with this approch .so it is good to go :) – Lithu T.V Jul 26 '13 at 06:10
  • Yep, I was looking for a magic bullet. But, I will go with this way. – Diego Barros Jul 26 '13 at 06:11