我打算使用 RetKit,但文档可能会更好。
如何根据请求参数为资源注册映射?不幸的是,我没有编写好的服务可以使用,所以我所有的资源都在里面,somexamplepath.com/api/
但询问一个是通过参数完成的,比如:
somexamplepath.com/api/?res=people
或somexamplepath.com/api/?res=machines
针对这种情况进行映射的最佳方法是什么?
我打算使用 RetKit,但文档可能会更好。
如何根据请求参数为资源注册映射?不幸的是,我没有编写好的服务可以使用,所以我所有的资源都在里面,somexamplepath.com/api/
但询问一个是通过参数完成的,比如:
somexamplepath.com/api/?res=people
或somexamplepath.com/api/?res=machines
针对这种情况进行映射的最佳方法是什么?
这种情况没有很好地涵盖,因为 RestKit 匹配的是 URL 路径,而不是查询参数。
您可以使用多个实例,RKObjectManager
其中每个实例将用于 API 内容的特定子集。
我还没有尝试过,但可以设置一个 2 阶段过程,其中 URL 路径用于最初匹配并将您带到RKDynamicMapping
. 在动态映射中,您可以使用RKObjectMappingMatcher
which 访问基于谓词的匹配,使用matcherWithPredicate:objectMapping:
. 如果该@metadata.URL
键对谓词可用,则可以匹配那里的所有查询参数。
您正在谈论具有不同查询字符串的相同资源。发送请求时只需添加查询字符串参数..即
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];
}];
}