[英]how to wait until the task is completed and then return a signal, reactive cocoa
-(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. 让我解释一下我要做什么。
endPoint
signal 我通过endPoint
信号包装endPoint url map
to extract url and do a service call ( service([NSURL URLWithString: endPoint])
) 使用map
提取URL并进行服务调用( service([NSURL URLWithString: endPoint])
) map
to extract info from step 2 and create info
data 使用map
从步骤2中提取信息并创建info
数据 updateVideoStateWith
with a callback 用回调执行updateVideoStateWith
result
返回包含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. 最终,当我订阅finalPackage
信号时,返回的初始化值为9
我意识到, updateVideoStateWith
回调将花费一些时间来返回结果。
My question is how can I force return [RACSignal return:result]
wait until the data is updated from callback block. 我的问题是如何强制return [RACSignal return:result]
等到从回调块更新数据。 I did tried takeUntilBlock but not sure how to use it. 我确实尝试过takeUntilBlock,但不确定如何使用它。 I also think about using switchToLatest but still no luck. 我也考虑使用switchToLatest,但还是没有运气。
Cross posting my answer from the GitHub issue: 从GitHub问题交叉发布我的答案:
- (RACSignal*)finalPackage {
return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
RACSignal *endPointSignal = [[DGConfiguration sharedInstance].apiConfiguration urlTemplate:DGAPIUrlLocalWatchList];
[[endPointSignal map:^id(NSString *endPoint) {
// map your endpoints to episodes and return the array of episodes
}] subscribeNext:^(NSArray* episodes) {
// Create your initial result array
[[DGManager sharedInstance] updateVideoStateWith:info callback:^(NSArray *response) {
// Do whatever work you need to do with the response to modify the result array
[subscriber sendNext:result];
[subscriber sendComplete];
}];
} error:^(NSError* error) {
[subscriber sendError:error];
]];
return nil;
}];
}
Note: if you're returning a RACSignal* when mapping from the endPoint NSString you'll want to flattenMap
instead of map
, flattenMap will flatten out the signal that is returned into the value it emits. 注意:如果从endPoint NSString映射时返回RACSignal *,则需要用flattenMap
代替map
,flattenMap将把返回的信号展平为它发出的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.