我在使用 RestKit 时遇到问题。
我配置将POST请求responseDescriptors
映射到with并将GET请求映射到with 。/users/login
generalResponseMapping
/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
- (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?
UPD
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
如果我注释掉 responseDescriptor 的添加,users/:userID
我可以成功获得正确的映射,并且网络模块将记录:
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"}
否则我会在 @autoreleasepool 块的末尾得到 EXC_BAD_ACCESS-[RKMapperOperation mapSourceRepresentationWithMappingsDictionary]
并且网络块日志记录保持安静。