Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuiAAPeres/8905610 to your computer and use it in GitHub Desktop.
Save RuiAAPeres/8905610 to your computer and use it in GitHub Desktop.
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