0

How can I post an array of objects to my server with RESTKit?

I have a custom object called Contact which have some properties like name, phone etc. I would like to send an array of these Contact objects to the server. The method I know for this is postObject:path:parameters:success:failure, but what object I put here? If I put Contact - how will it know it is an array? and if I put NSArray, how will it know it is a Contact?

My Contact object header file is:

@interface Contact : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *phone;
@property (nonatomic) NSInteger order;
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;

@end

my response mapping is:

RKObjectMapping *personMapping = [RKObjectMapping mappingForClass:[Contact class]];
    [personMapping addAttributeMappingsFromDictionary:@{
                                   @"username": @"name",
                                   @"firstname" : @"firstName",
                                   @"lastname" : @"lastName",
                  }];

my response descriptor is:

RKResponseDescriptor *personResponseDescriptorForArrayOfPhones =
[RKResponseDescriptor responseDescriptorWithMapping:personMapping
                                             method:RKRequestMethodANY
                                        pathPattern:@"getUsersInfoByPhones"
                                            keyPath:nil
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];

my request mapping is:

RKObjectMapping *personRequestMapping = [RKObjectMapping requestMapping ]; 
    [personRequestMapping addAttributeMappingsFromDictionary:@{
                         @"name": @"username",
                         @"firstName" : @"firstName",
                         @"lastName" : @"lastName",
                         @"phone" : @"usernames"
              }];

my request descriptor is:

RKRequestDescriptor *personRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:personRequestMapping
                                                                                     objectClass:[Contact class]
                                                                                     rootKeyPath:nil
                                                                                          method:RKRequestMethodAny];

Does this implementation looks good?

Also, this array I want to send to the server could be very big, around 200-1000 objects. Is that possible with RESTKit?

Update: Actually, I would prefer to send an array of strings (which would be phone numbers), and get from the server the Contact objects I have. How can I set RESTKit to post the array of strings and to expect a response of an array of Contact objects?

The json I need to send looks like this:

{
    "usernames":["11","22"]
}

the json I expect to get is:

[
  {
    "_id" : "53e23a54e811310000955f70",
    "profileUpdatesCounter" : 3,
    "lastname" : "SMITH",
    "firstname" : "BOB",
    "username" : "11"
  }
]
bobsacameno
  • 765
  • 3
  • 10
  • 25
  • 1
    Possibility depends on context - device, data availability, memory availability. Try. If you have a problem, show the code you used and the server log of what it received. Can you do the same thing from a test system (browser plugin or similar)? – Wain Aug 10 '14 at 11:36
  • what would be the object to put in the postObject: method? Would it be a NSArray? – bobsacameno Aug 10 '14 at 11:47
  • could you please explain more about how to exactly send the array? – bobsacameno Aug 11 '14 at 12:54
  • You just pass an array. You need to have the associated request descriptors configured. Update the question to show your source data, mappings and desired JSON, together with what you actually get out. (RestKit looks into the array to see what objects are there to know what to do) – Wain Aug 11 '14 at 12:57
  • This does not work for me. My server expects the to get an array of usernames in the body of the request. I think that the postObject: method does not do it exactly. Do you know how can I do it? Or what should I tell me server to expect? – bobsacameno Aug 13 '14 at 17:28
  • So you're currently sending too much data (you don't show the JSON you want and the JSON you get...). Try using a nil key path mapping / searching https://www.google.com/search?q=restkit+post+array+of+strings. – Wain Aug 13 '14 at 19:49
  • I've updated the question with the JSON I get/send. I'm already using nil in the key path. I've already searched the web, but didn't find an explanation – bobsacameno Aug 13 '14 at 19:53
  • nil key path mapping, not response descriptor. – Wain Aug 13 '14 at 20:02
  • what do you mean? where do I specify this? – bobsacameno Aug 13 '14 at 20:06
  • See the answer http://stackoverflow.com/questions/18004327/restkit-mapping-for-array-with-no-keypath – Wain Aug 15 '14 at 09:16

0 Answers0