服务器团队希望将一个Item类的2个PUT请求分开,如下所示 -
PUT '<server>/item/:itemId'
和
PUT '<server>/item/:itemId/like'
我在初始化程序中使用以下代码:
RKObjectManager *sharedRKObjectManager = [RKObjectManager sharedManager];
RKManagedObjectStore *managedObjectStore = [sharedRKObjectManager managedObjectStore];
// Create the mapping for the News entity.
RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:ENTITY_ITEM inManagedObjectStore:managedObjectStore];
[responseMapping addAttributeMappingsFromDictionary:@{
@"id": @"itemId",
}];
// Create a response descriptor and add it to the RKObjectManager object.
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodAny
pathPattern:UPDATE_ITEM_URL
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[sharedRKObjectManager.router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:UPDATE_ITEM_URL method:RKRequestMethodAny]];
[sharedRKObjectManager addResponseDescriptor:responseDescriptor];
接下来是电话:
+(void)updateItem:(Item*)item
withParams:(NSDictionary*)params
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
{
[CMLRKSharedManager setUpHeaders];
[[RKObjectManager sharedManager] putObject:item path:nil parameters:params success:success failure:failure];
}
我的问题是 - 我可以对这两个请求使用相同的功能吗?我能以任何其他方式附加网址吗?
TIA!
答案 0 :(得分:1)
在这种情况下,请勿使用RKRoute
。致电put...
时,请明确提供相应的路径。
请注意,您还应该有2个响应描述符,因为路径模式不同。
答案 1 :(得分:0)
万一它可以帮助任何人..这里是执行此操作的代码
NSString *path = [NSString stringWithFormat:@"item/%@/like", itemId];
[CMLRKSharedManager updateItem:item params:nil path:path success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Displaying user info %@", [mappingResult firstObject]);
} failure:nil];