这是我的路由设置:
[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 的映射被用于序列化步骤,那么为什么不将它用于对应的反序列化,以及如何使用它?