Created
January 23, 2015 00:11
-
-
Save hyperspacemark/e8dc9aacb967f24eb77d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface LocationController () <CLLocationManagerDelegate> | |
@property (nonatomic) CLLocationManager *locationManager; | |
@end | |
@implementation LocationController | |
- (RACSignal *)locationUpdate | |
{ | |
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
[self.locationManager startUpdatingLocation]; | |
[[self locationUpdated] subscribeNext:^(id x) { | |
[subscriber sendNext:x]; | |
[subscriber sendCompleted]; | |
}]; | |
[[self locationUpdateError] subscribeError:^(NSError *error) { | |
[subscriber sendError:error]; | |
}]; | |
return [RACDisposable disposableWithBlock:^{ | |
[self.locationManager stopUpdatingLocation]; | |
}]; | |
}]; | |
} | |
- (RACSignal *)locationUpdated | |
{ | |
@weakify(self) | |
return [[[[[self rac_signalForSelector:@selector(locationManager:didUpdateLocations:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id(RACTuple *tuple) { | |
NSArray *locations = tuple.second; | |
return locations.lastObject; | |
}] filter:^BOOL(CLLocation *location) { | |
return !location.isStale; | |
}] take:1] doCompleted:^{ | |
@strongify(self) | |
[self.locationManager stopUpdatingLocation]; | |
}]; | |
} | |
- (RACSignal *)locationUpdateError | |
{ | |
return [[[self rac_signalForSelector:@selector(locationManager:didFailWithError:) fromProtocol:@protocol(CLLocationManagerDelegate)] map:^id(RACTuple *tuple) { | |
return tuple.second; | |
}] take:1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment