Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres

RuiAAPeres/gist:8905610

Last active Aug 29, 2015
Embed
What would you like to do?
RACSignal + Filter + Timer
// The goal is to make a server call every 5 minutes, but I want to make sure the user has at least moved 500 meters, how would go after this:
[RACObserve(self, userLocation) filter:^BOOL(CLLocation *newLocation)
{
return [newLocation distanceFromLocation:self.userLocation]>APH500Meters;
}];
// I was thinking on creating a scheduler so I got this:
RACScheduler *scheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault];
[scheduler afterDelay:60*5 schedule:^{
[[RACObserve(self, userLocation) filter:^BOOL(CLLocation *newLocation)
{
return [newLocation distanceFromLocation:self.userLocation]>APH500Meters;
}] doNext:^(id x) {
// Make the call here;
}];
}];
// But I feeld there must be a better way of doing it.
// Was able to put it to work like this:
RACScheduler *scheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault];
[scheduler after:[NSDate date] repeatingEvery:10 withLeeway:0 schedule:^{
[[RACObserve(self, userLocation) filter:^BOOL(CLLocation *newLocation)
{
return [newLocation distanceFromLocation:self.userLocation]>APH500Meters;
}] doNext:^(id x) {
// Make the call here;
NSLog(@"asd");
}];
}];
// Changed it to this:
RACSignal *timerSignal = [RACSignal interval:5*60 onScheduler:[RACScheduler mainThreadScheduler]];
[[RACSignal combineLatest:@[timerSignal, RACObserve(self, userLocation)]] subscribeNext:^(id x)
{
NSLog(@"Bang");
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment