gpt4 book ai didi

ios - Restkit:在一个请求中映射到两个responseDescriptor

转载 作者:行者123 更新时间:2023-11-29 12:58:59 25 4
gpt4 key购买 nike

我有一个请求给我两部分数据,json 看起来像这样

{
"banner_content":
[
{
"activi_id":"1",
"activi_pic":
},
{
"activi_id":"2",
"activi_pic":
},
],
"categories":
[
{...},
{...},
]
}

当我编写 responseDescriptor 来映射数据时,我发现我必须定义一个包含 banner_content 数组和 categories 数组的模型(我不想)否则我必须写两个 responseDescriptors 来做,当请求完成时,我必须从 (RKMappingResult *)mappingResult 中获取两部分数据然后获取像 [mappingResult objectForKey:@"banner_content"][mappingResult objectForKey:@"categories"] 这样的数组很奇怪

下面是我的代码文件:RCategory.m

@implementation RCategory


+ (NSDictionary *)_mapping {
return @{@"title" : @"title"};
}

+ (RKObjectMapping *)mapping {

// Setup our object mappings
RKObjectMapping *categoryMapping = [RKObjectMapping mappingForClass:[self class]];
[categoryMapping addAttributeMappingsFromDictionary:[[self class] _mapping]];

RKObjectMapping *itemMapping = [RCategoryItem mapping];

RKRelationshipMapping* relationShipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"content"
toKeyPath:@"items"
withMapping:itemMapping];
[categoryMapping addPropertyMapping:relationShipMapping];

return categoryMapping;
}

@end

文件:RAd.m 被忽略

文件:viewController.m

- (void)loadCategory {
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];

RKObjectMapping *categoryMapping = [RCategory mapping];

RKResponseDescriptor *categoryResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"category"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectMapping *adsMapping = [RAd mapping];
RKResponseDescriptor *adResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:adsMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"banner_content"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

//** DO I MUST ADD TWO RESPONSE_DESCRIPTOR FOR ONE REQUEST??**
[objectManager addResponseDescriptor:categoryResponseDescriptor];
[objectManager addResponseDescriptor:adResponseDescriptor];

[objectManager getObjectsAtPath:RPATH(CATEGORY_PATH)
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSArray* statuses = [mappingResult array];
[self.categories addObjectsFromArray:statuses];
if ([self isViewLoaded]) {
[self.tableView reloadData];
}
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];

}

有没有人有更好的方法来解决我的问题?顺便说一句,我在模型类中编写了 -mapping,这是我能想到的最简单和最好的方法我想知道你是如何写映射的。

最佳答案

您需要 2 个响应描述符,因为响应有 2 个逻辑上独立的部分。这可以。如果您使用的是 Core Data,那么您就不必担心,因为您的所有对象都将简单地保存到上下文中,您可以根据需要获取它们。使用对象映射,如果您不关心分组,那么您可以从映射结果中获取所有对象的数组。

从模型对象返回的数据创建映射很好 - 但它确实限制了你,因为每个目标键只能有一个源键。当您对具有不同 key 的同一对象有不同的响应时会发生什么,这意味着与其他 key 相同(希望您没有,但这完全取决于服务器 API)。

关于ios - Restkit:在一个请求中映射到两个responseDescriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20257363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com