Best practice for handling chain requests using reactive cocoa
I'm new to reactive words, so please help find the best solution for this scenario: I'm working with Youtube API . I want to load VideoCategories and then get one video for each category, then download a thumbnail for each video, copy it to the model and only then send a signal to table mode to reload the data. I am requesting 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 do I send a query for each listModel.items
and then concatenate the result and then pass the table view?
+3
source to share
1 answer
Okay, for everyone who is still surprised. Explanation in a 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
}];
0
source to share