對于失敗的信號沒有處理
- (void)setup {
? ? //定位
? ? _locationManager = [[DellocCLLocationManager alloc] init];
? ? _geoCoder = [[CLGeocoder alloc] init];
? ? _locationManager.delegate = self;
? ? _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
? ? _locationManager.distanceFilter = 1.0;
? ? _currentLocation=[[CLLocation alloc]initWithLatitude:31.232032 longitude:121.476173];
?? ?
? ? @weakify(self);
?? ?
? ? [[[[[self authorizedSignal] filter:^BOOL(id value) {
? ? ? ? return [value boolValue];
? ? }] flattenMap:^RACStream *(id value) {
?? ? ? ? @strongify(self);
? ? ? ? return [[[[[[self rac_signalForSelector:@selector(locationManager:didUpdateLocations:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id(id value) {
? ? ? ? ? ? CLLocation *firstLocation= [value[1] firstObject];
? ? ? ? ? ? CLLocationCoordinate2D coordinate = [JZLocationConverter wgs84ToGcj02:firstLocation.coordinate];
? ? ? ? ? ? CLLocation *correctLocation=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
? ? ? ? ? ? return correctLocation;
? ? ? ? }] merge:[[self rac_signalForSelector:@selector(locationManager:didFailWithError:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id(id value) {
? ? ? ? ? ? return [RACSignal error:value[1]];
? ? ? ? }]] take:1] initially:^{
? ? ? ? ? ? @strongify(self);
? ? ? ? ? ? [self.locationManager startUpdatingLocation];
? ? ? ? }] finally:^{
? ? ? ? ? ? @strongify(self);
? ? ? ? ? ? [self.locationManager stopUpdatingLocation];
? ? ? ? }];
? ? }] flattenMap:^RACStream *(id value) {
?? ? ? ?
? ? ? ? if (![value isKindOfClass:[CLLocation class]]) {
? ? ? ? ? ? return? [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
? ? ? ? ? ? ? ? [subscriber sendNext:@"定位失敗"];
? ? ? ? ? ? ? ? [subscriber sendCompleted];
? ? ? ? ? ? ? ? return [RACDisposable disposableWithBlock:^{
?? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }];
? ? ? ? ? ? }];
? ? ? ? }
? ? ? ? CLLocation *firstLocation= value;
? ? ? ? return? [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
? ? ? ? ? ? @strongify(self);
? ? ? ? ? ? [self.geoCoder reverseGeocodeLocation:firstLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
? ? ? ? ? ? ? ? if (error) {
? ? ? ? ? ? ? ? ? ? [subscriber sendNext:error];
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? [subscriber sendNext:[placemarks firstObject]];
? ? ? ? ? ? ? ? ? ? [subscriber sendCompleted];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }];
? ? ? ? ? ? return [RACDisposable disposableWithBlock:^{
?? ? ? ? ? ? ? ?
? ? ? ? ? ? }];
? ? ? ? }];
? ? }] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
}
2017-03-30
這里判斷不是 CLLocation 類型來判斷的, 可以使用 sendError 。我也忘了錄課時候為什么使用sendnext了。
2017-04-18
我這邊這么處理的..
??RACSignal *locationSignal = [[[[self authorizedSignal] filter:^BOOL(id? _Nullable value) {
? ? ? ? //判斷是否有權限
? ? ? ? return [value boolValue];
? ? }] flattenMap:^__kindof RACSignal * _Nullable(id? _Nullable value){
? ? ? ? //監聽地址刷新的狀態
? ? ? ? return [[[[[[self rac_signalForSelector:@selector(locationManager:didUpdateLocations:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id _Nullable(id? _Nullable value) {
? ? ? ? ? ? return value[1];
? ? ? ? ? ? //混合錯誤監聽
? ? ? ? }] merge:[[self rac_signalForSelector:@selector(locationManager:didFailWithError:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id _Nullable(id? _Nullable value) {
? ? ? ? ? ? //返回錯誤
? ? ? ? ? ? return [RACSignal error:value[1]];
? ? ? ? }]] take:1] initially:^{
? ? ? ? ? ? //initially 在所有之前開始
? ? ? ? ? ? [self.manager startUpdatingLocation];
? ? ? ? }] finally:^{
? ? ? ? ? ? //finally 處理完之后
? ? ? ? ? ? [self.manager stopUpdatingLocation];
? ? ? ? }];
? ? }] flattenMap:^__kindof RACSignal * _Nullable(id? _Nullable value) {
?? ? ? ?
? ? ? ? return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>? _Nonnull subscriber) {
? ? ? ? ? ? if ([[value class] isSubclassOfClass:[RACErrorSignal class]]) {
? ? ? ? ? ? ? ? [subscriber sendError:value];
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? CLLocation *location = [value firstObject];
? ? ? ? ? ? ? ? [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
? ? ? ? ? ? ? ? ? ? if (error) {
? ? ? ? ? ? ? ? ? ? ? ? [subscriber sendError:error];
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? [subscriber sendNext:[placemarks firstObject]];
? ? ? ? ? ? ? ? ? ? ? ? [subscriber sendCompleted];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }];
? ? ? ? ? ? }
? ? ? ? ? ? return [RACDisposable disposableWithBlock:^{
? ? ? ? ? ? }];
? ? ? ? }];
? ? }];
2017-03-29
和你不太一樣的地方就是我把你手動該的 RACStream 的返回值改成了 id 下面貼上我的代碼吧:
2017-03-29
我用你的代碼實驗一下...走了啊..我把
locationManager:didUpdateLocations:
這個方法的時候你 return 的
correctLocation
這個位置的值直接用 @222 這么一個 NSNumber 類型替換了, 運行的時候走了定位失敗...