Skip to content

Instantly share code, notes, and snippets.

@hyperspacemark
Created January 23, 2015 00:11
Show Gist options
  • Save hyperspacemark/e8dc9aacb967f24eb77d to your computer and use it in GitHub Desktop.
Save hyperspacemark/e8dc9aacb967f24eb77d to your computer and use it in GitHub Desktop.
@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