-(RACSignal*)finalPackage {
RACSignal *endPoint = [[DGConfiguration sharedInstance].apiConfiguration
urlTemplate:DGAPIUrlLocalWatchList];` // 1.
return [[endPointRequestSignal map:^id(NSString *endPoint) { // 2.
return service([NSURL URLWithString: endPoint]);
}].flatten map:^id(NSArray *episodes) { // 3.
NSMutableArray *info= [NSMutableArray array];
__block NSArray *result=@[@(9)]; // test value is 9, result will be updated during callback block
[episodes enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL *stop) {
[info addObject:@{@"id":item[@"id"],@"links":item[@"links"]}];
}];
[[DGManager sharedInstance] updateVideoStateWith:info callback:^(NSArray *response) { // 4.
dispatch_async(dispatch_get_main_queue(), ^{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
result = [[response sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]] copy];
});
}];
return [RACSignal return:result]; // 5.
}].flatten;
}
Lets me explain what I am trying to do.
- I wrap the endPoint url via
endPointsignal - Using
mapto extract url and do a service call (service([NSURL URLWithString: endPoint])) - Using
mapto extract info from step 2 and createinfodata - Do
updateVideoStateWithwith a callback - Return a signal which contains
result
Eventually, when I subcribe to finalPackage signal, the return is initialized value which is 9.I realize that the updateVideoStateWith call back will take time to return the result.
My question is how can I force return [RACSignal return:result] wait until the data is updated from callback block. I did tried takeUntilBlock but not sure how to use it. I also think about using switchToLatest but still no luck.
Cross posting my answer from the GitHub issue:
Note: if you're returning a RACSignal* when mapping from the endPoint NSString you'll want to
flattenMapinstead ofmap, flattenMap will flatten out the signal that is returned into the value it emits.