我遇到了RestKit的问题。
我配置responseDescriptors
将{strong> POST 请求映射到/users/login
,generalResponseMapping
和 GET 请求/users/:userID
userMapping
。
初始化代码:
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
...
RKObjectMapping *generalResponseMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[generalResponseMapping addAttributeMappingsFromArray:@[ @"status", @"token" ]];
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
userMapping.identificationAttributes = @[ @"userID" ];
[userMapping addAttributeMappingsFromArray:@[ @"avatar", @"name", @"email" ]];
void(^configureResponse)(RKMapping *, RKRequestMethod, NSString *, NSString *, NSIndexSet *) =
^(RKMapping *mapping, RKRequestMethod method, NSString *pattern, NSString *keyPath, NSIndexSet *statusCodes)
{
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:method
pathPattern:pattern
keyPath:keyPath
statusCodes:statusCodes];
[objectManager addResponseDescriptor:responseDescriptor];
};
NSIndexSet *codes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
configureResponse(generalResponseMapping, RKRequestMethodPOST, @"users/login", nil, codes);
configureResponse(userMapping, RKRequestMethodGET, @"users/:userID", nil, codes);
...
但是当我尝试向/users/login
发送 POST 请求时。
- (void)loginWithUserdata:(NSDictionary *)userdata
{
NSString *path = @"users/login";
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager postObject:nil
path:path
parameters:userdata
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
...
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
...
}];
}
我收到错误,因为RestKit正在尝试使用generalResponse
userMapping
对象
restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
status = 0;
token = "some_fake_token";
} with mapping <RKEntityMapping:0x9bb9040 objectClass=User propertyMappings=(
"<RKAttributeMapping: 0x9bc5980 avatar => avatar>",
"<RKAttributeMapping: 0x9b64dc0 name => name>",
"<RKAttributeMapping: 0x9bc5700 email => email>",
"<RKAttributeMapping: 0x9bc6a20 id => userID>",
)>
如何使用适当的映射映射来自两个路径的响应,而无需在每次请求之前重新排列objectManagers responseDescriptors?
RestKit网络模块的日志:
restkit.network:RKObjectRequestOperation.m:178 POST 'http://api.myapp.com/users/login':
request.headers={
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";
"User-Agent" = "MyApp/666 (iPhone Simulator; iOS 7.0; Scale/2.00)";
}
request.body=email=t&password=t
如果我评论为users/:userID
添加了responseDescriptor,我可以成功地获得正确的映射,并且网络模块将记录:
restkit.network:RKObjectRequestOperation.m:248 POST 'http://api.myapp.com/users/login' (200 OK / 1 objects) [request=0.0286s mapping=7.6855s total=7.7190s]:
response.headers={
"Cache-Control" = "max-age=0, private, must-revalidate";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 07 Oct 2013 23:18:06 GMT";
Etag = "\"555dbdf216a0f30229810af9411ad7c9\"";
"Keep-Alive" = "timeout=20";
Server = nginx;
Status = "200 OK";
"Transfer-Encoding" = Identity;
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Request-Id" = "07803850-cfe3-4ccb-9a0d-46be980b0537";
"X-Runtime" = "0.004017";
"X-UA-Compatible" = "chrome=1";
"X-XSS-Protection" = "1; mode=block";
}
response.body={"status":0,"token":"some_fake_token"}
否则我在-[RKMapperOperation mapSourceRepresentationWithMappingsDictionary]
的@autoreleasepool块结尾处获得EXC_BAD_ACCESS,并且网络块记录保持安静。
答案 0 :(得分:0)
不确定请求类型是否应提供所需的拆分。这一行:
NSString *path = @"/users/login";
更改为:
NSString *path = @"users/login";
当RestKit尝试匹配它们时,在路径模式的一部分而不是另一部分(在响应描述符上)添加斜杠会导致失败。