代码之家  ›  专栏  ›  技术社区  ›  Realinstomp

在一个表视图中按日期排序的1个API源和1个RSS源[已关闭]

  •  0
  • Realinstomp  · 技术社区  · 11 年前

    这之前对我来说非常有效: 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 回复  |  直到 8 年前
        1
  •  0
  •   Realinstomp    11 年前

    我将尝试在 loadTwoWithSuccess 因为这似乎更有意义-