我正在努力使用RetKit,但文档可能会更好。
如何根据请求参数注册资源的映射?
不幸的是,我没有很好的书面服务,所以我的所有资源都在somexamplepath.com/api/
但是问一个是由参数完成的:
somexamplepath.com/api/?res=people
或somexamplepath.com/api/?res=machines
为这种情况做映射的最佳方法是什么?
答案 0 :(得分:2)
这种情况没有得到很好的解决,因为RestKit与URL路径匹配,而不是查询参数。
您可以使用RKObjectManager
的多个实例,其中每个实例都用于API内容的特定子集。
我还没试过这个,但是可以设置一个2阶段的流程,其中URL路径最初用于匹配,并将您带到RKDynamicMapping
。在动态映射中,您可以使用RKObjectMappingMatcher
,使用matcherWithPredicate:objectMapping:
可以访问基于谓词的匹配。如果谓词可以使用@metadata.URL
键,则可以匹配那里的所有查询参数。
答案 1 :(得分:1)
您正在讨论具有不同查询字符串的相同资源。只需在发送请求时添加查询字符串参数即..
NSDictionary *params = @{@"res":@"people"};
[[RKObjectManager sharedManager] getObjectsAtPath:@"api/"
parameters:params
success:^(RKObjectRequestOperation *operation,
RKMappingResult *mappingResult) {
// blah blah
这是我自己的代码中更全面的示例..我完全理解整个“文档可能更好”..注意我有两个实体映射(对于两个不同的实体)..事件和与会者:
// Configure the object manager
_objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://mydomain.com/api/v1"]];
_objectManager.managedObjectStore = _managedObjectStore;
[RKObjectManager setSharedManager:_objectManager];
_objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
RKEntityMapping *eventEntityMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:_managedObjectStore];
[eventEntityMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"resource_uri": @"resource_uri",
@"title": @"title",
@"city": @"city",
@"from_date": @"from_date",
@"user": @"user_id"}];
eventEntityMapping.identificationAttributes = @[ @"id" ];
RKResponseDescriptor *eventResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventEntityMapping pathPattern:@"event/" keyPath:@"objects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[_objectManager addResponseDescriptor:eventResponseDescriptor];
RKEntityMapping *attendeeEntityMapping = [RKEntityMapping mappingForEntityForName:@"Attendee" inManagedObjectStore:_managedObjectStore];
[attendeeEntityMapping addAttributeMappingsFromDictionary:@{
@"id": @"id",
@"order.first_name": @"first_name",
@"order.last_name": @"last_name",
@"unique": @"order_ticket_unique_code",
@"ticket.event": @"event_id",
@"ticket.description": @"ticket_description",
@"ticket.perk": @"ticket_perk",
@"ticket.price": @"ticket_price"}];
attendeeEntityMapping.identificationAttributes = @[ @"id" ];
RKResponseDescriptor *attendeeResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:attendeeEntityMapping pathPattern:@"orderticket/search/" keyPath:@"objects" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
_objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
[_objectManager addResponseDescriptor:attendeeResponseDescriptor];
稍后我只需获取一个与会者资源(在api中称为orderticket ..无论如何)
- (void)downloadAttendees
{
NSDictionary *params = @{@"q":_eventId};
[[RKObjectManager sharedManager] getObjectsAtPath:@"orderticket/search/" parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[self.refreshControl endRefreshing];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}];
}
我还获取了一个事件资源(事件资源不接受查询字符串):
- (void)downloadEvents
{
[[RKObjectManager sharedManager] getObjectsAtPath:@"event/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[self.refreshControl endRefreshing];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}];
}