在一个表视图中按日期排序的1个API源和1个RSS源

时间:2014-09-01 23:02:32

标签: ios arrays uitableview rss restkit

这对我来说非常合适: Get one NSArray

现在,我的第二个来源是RSS而不是API

我假设我需要以某种方式更改loadTwoWithSuccess,但我无法弄清楚如何。

然后从那里我肯定需要按照第二个来源的新日期以某种方式更新sortCombinedModel

你能帮我解决这个问题吗?谢谢!

两个异步调用:

- (void)loadOneWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                   failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure {

    NSString *apikey = @kCLIENTKEY;
    NSDictionary *queryParams = @{@"apikey" : apikey};
    NSString *path = [NSString stringWithFormat:@"v1/n/?limit=4&leafs=%@&themes=%@", leafAbbreviation, themeID];

    [self.eObjectManager getObjectsAtPath:path parameters:queryParams success:success failure:failure];
}

- (void)loadTwoWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                   failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure {

    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://se.com/rss/c-b/"];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
    NSLog(@"Success 2: %@", success);   
}

将源加载为单一组合模型:

- (void)loadMedia {

    self.combinedModel = [NSMutableArray array];

    [self loadOneWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        [self.combinedModel addObjectsFromArray:mappingResult];

        // here's the trick.  call API2 here.  Doing so will serialize these two requests
        [self loadTwoWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

            [self.combinedModel addObjectsFromArray:mappingResult];
            [self sortCombinedModel];
            [self.tableView reloadData];

        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"No?: %@", error);
        }];

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"No?: %@", error);
    }];
}

排序组合模型:

- (void)sortCombinedModel {
    [self.combinedModel sortUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *dateA, *dateB;
        dateA = ([a isKindOfClass:[Feed self]])? ((Feed *)a).published : ((Data *)a).created_time;
        dateB = ([b isKindOfClass:[Feed self]])? ((Feed *)b).published : ((Data *)b).created_time;
        return [dateA compare:dateB];
    }];
}

新表格视图:

id model = self.combinedModel[indexPath.row];
if ([model isKindOfClass:[Feed self]) {
    Feed *feed = (Feed *)model;
    // configure cell with feed
} else {
    Data *data = (Data *)model;
    // configure cell with data
}

1 个答案:

答案 0 :(得分:0)

我将尝试在loadTwoWithSuccess内部使用RestKit作为RSS Feed,因为这似乎更有意义 -