我正在使用ReactiveCocoa开发一个小应用程序来进行一些本地搜索。它看起来像这样:
- (void)awakeFromNib {
...
[[[[RACObserve(self, currentLocation) ignore:nil]
flattenMap:^(CLLocation *newLocation) {
return [self updateRestroomArray];
}] deliverOn:RACScheduler.mainThreadScheduler]
subscribeError:^(NSError *error) {
NSLog(@"ERROR : %@", error);
}];
...
}
// Store result
- (RACSignal *)updateRestroomArray
{
return [[self searchRestroomForCoordinate:self.currentLocation.coordinate]
doNext:^(NSArray *mapItems) {
self.restroomArray = mapItems;
}];
}
// local search
- (RACSignal *)searchRestroomForCoordinate:(CLLocationCoordinate2D)coordinate
{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
MKCoordinateRegion searchRegion = MKCoordinateRegionMakeWithDistance(coordinate, 300, 300);
self.restroomSearchRequest.region = searchRegion;
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:self.restroomSearchRequest];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (error) {
[subscriber sendError:error];
} else if (0 == response.mapItems.count) {
NSError *noResultError = [[NSError alloc] initWithDomain:@"com.gnou.emergency.search" code:100 userInfo:@{@"testKey":@"testValue"}];
[subscriber sendError:noResultError];
} else {
[subscriber sendNext:response.mapItems];
}
[subscriber sendCompleted];
}];
return [RACDisposable disposableWithBlock:^{
[localSearch cancel];
}];
}];
}
当我在 awakeFromNib 中使用 flattenMap 时,它运行正常,我会得到结果,但当我使用' map '时, localSearch 只是不做搜索工作,为什么?我看不出任何需要“扁平化”的事情。
答案 0 :(得分:0)
我想我明白了,
所以updateRestroomArray
实际上创建了一个信号信号,如果我使用'-map:',subscribeError:
中的awakeFromNib
将订阅此信号信号,但{{1}没有订阅者所以它永远不会做它的工作