RESTkit – 将大量对象发布到服务器
如何使用RESTKit将对象数组发布到我的服务器?
我有一个名为Contact
的自定义对象,有一些像name
, phone
等属性。我想发送这些Contact
对象的数组到服务器。 我知道这个方法是postObject:path:parameters:success:failure
,但是我把这个对象放在这里? 如果我把Contact
– 它将如何知道它是一个数组? 如果我把NSArray
,它怎么知道它是一个Contact
?
我的Contact
对象头文件是:
@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
我的反应映射是:
RKObjectMapping *personMapping = [RKObjectMapping mappingForClass:[Contact class]]; [personMapping addAttributeMappingsFromDictionary:@{ @"username": @"name", @"firstname" : @"firstName", @"lastname" : @"lastName", }];
我的回复描述符是:
RKResponseDescriptor *personResponseDescriptorForArrayOfPhones = [RKResponseDescriptor responseDescriptorWithMapping:personMapping method:RKRequestMethodANY pathPattern:@"getUsersInfoByPhones" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
我的请求映射是:
RKObjectMapping *personRequestMapping = [RKObjectMapping requestMapping ]; [personRequestMapping addAttributeMappingsFromDictionary:@{ @"name": @"username", @"firstName" : @"firstName", @"lastName" : @"lastName", @"phone" : @"usernames" }];
我的请求描述符是:
RKRequestDescriptor *personRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:personRequestMapping objectClass:[Contact class] rootKeyPath:nil method:RKRequestMethodAny];
这个实现看起来不错吗?
另外,我想发送给服务器的这个数组可能非常大,大约有200-1000个对象。 这可能与RESTKit?
更新 :其实,我宁愿发送一个string数组(这将是电话号码),并从服务器获取我有的Contact
对象。 我如何设置RESTKit来发布string数组,并期待一个Contact
对象数组的响应?
我需要发送的JSON如下所示:
{ "usernames":["11","22"] }
我期望得到的json是:
[ { "_id" : "53e23a54e811310000955f70", "profileUpdatesCounter" : 3, "lastname" : "SMITH", "firstname" : "BOB", "username" : "11" } ]