继承我的路由设置:
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[UserProfile class] pathPattern:@"/api/v1/users" method:RKRequestMethodPOST]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[UserProfileAndStatistics class] pathPattern:@"/api/v1/profile/:userId" method:RKRequestMethodGET]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[PrivateUserProfile class] pathPattern:@"/api/v1/privateprofile/" method:RKRequestMethodGET]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[PrivateUserProfile class] pathPattern:@"/api/v1/privateprofile/" method:RKRequestMethodPOST]];
以下是我如何注册映射:
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:nil];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
//the serialisation step
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[mapping inverseMapping] objectClass:[mapping objectClass] rootKeyPath:nil];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
最后我打电话给:
[[RKObjectManager sharedManager] postObject:userProfile path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) { ... }
哪个帖子正确发布,我得到一个HTTP状态码200,然后数据进入服务器。这意味着序列化步骤正在运行。当响应回来时,我收到一个错误:
Adding mapping error: Expected an object mapping for class of type 'PrivateUserProfile', provider returned one for 'UserProfileAndStatistics'
现在PrivateUserProfile
和UserProfileAndStatistics
的映射非常相似
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[PrivateUserProfile class]];
[mapping addAttributeMappingsFromDictionary:@{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Email" : @"emailAddress",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar"
}];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[UserProfileAndStatistics class]];
[mapping addAttributeMappingsFromDictionary:@{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar",
@"Posts" : @"posts",
@"Followers" : @"followers",
@"Following" : @"following"
}];
但为什么RestKit会选择其中一个呢?如何成功调试? RKRoute和RKObjectMapping之间是否存在关系。 PrivateUserProfile的映射用于序列化步骤,那么为什么它不用于对应的反序列化,以及如何使它被使用?
答案 0 :(得分:4)
这里需要两个响应描述符 - 一个用于类/api/v1/privateprofile/
的路径PrivateUserProfile
,另一个用于类/api/v1/profile/:userId
的路径UserProfileAndStatistics
:
[manager addResponseDescriptorsFromArray:@[
[RKResponseDescriptor responseDescriptorWithMapping:privateUserProfileMapping
pathPattern:@"/api/v1/privateprofile/"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)],
[RKResponseDescriptor responseDescriptorWithMapping:userProfileAndStatisticsMapping
pathPattern:@"/api/v1/profile/:userId"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]
]];