RESTKit - post a large array of objects to server

时间秒杀一切 提交于 2019-12-20 07:25:15

问题


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"
  }
]

来源:https://stackoverflow.com/questions/25228098/restkit-post-a-large-array-of-objects-to-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!