Last active
August 29, 2015 13:56
-
-
Save RuiAAPeres/8905610 to your computer and use it in GitHub Desktop.
RACSignal + Filter + Timer
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
// 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