Search code examples
iosobjective-cafnetworking-2reactive-cocoa

The best practice to handle chaining requests using reactive cocoa


I'm new in reactive words, so, please help to find the best solution for this scenario: I work with Youtube API. I want to load VideoCategories, then get one top video for each category, then load thumbnail for each video, accumulate it into model, and only then send signal to table view to reload data. I request categories like this:

[[[TRYoutubeManager manager] rac_GET:@"videoCategories" parameters:parameters] map:^id(id responseObject) {
    TRYoutubeListResponseModel *listModel =
    [MTLJSONAdapter modelOfClass:[TRYoutubeListResponseModel class] fromJSONDictionary:responseObject error:nil];
    listModel.items = [[listModel.items.rac_sequence filter:^BOOL(TRYoutubeVideoCategoryModel *categoryModel) {
      return categoryModel.assignable;

    }] array];
    return listModel;
  }];

So, how to send request for each listModel.items and then combine the result, and then signal the table view?


Solution

  • Ok, for everybody who still wonder. Explanation in more abstract way:

    // You get your list ob objects here
    [[[Manager getList] flattenMap:^RACStream *(NSArray *yourList) {
      NSMutableArray *listItemsSignals = [NSMutableArray array];
      for (ItemClass *item in yourList) {
        //Something that produces signals
        RACSignal *itemSignal = [item imageSignal];
        [listItemsSignals addObject: itemSignal]
      }
    
      return [RACSignal combineLatest:listItemsSignals];
    }] subscribeNext:^(RACTuple *values) {
      // All your values are here
    }];