Skip to content

Instantly share code, notes, and snippets.

@joshaber
Last active December 15, 2015 07:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshaber/5226626 to your computer and use it in GitHub Desktop.
Save joshaber/5226626 to your computer and use it in GitHub Desktop.
ReactivePersistence?
// The basics
[[self.connection fetch:@"the-answer"] subscribeNext:^(NSNumber *x) {
NSLog(@"The answer is %@", x);
}];
[[self.connection writeObject:@42 forKey:@"the-answer"] subscribeCompleted:^{
NSLog(@"Wrote it.");
}];
// Signal for changes to the object at a key
[[self.connection signalForKey:@"the-answer"] subscribeNext:^(NSNumber *x) {
NSLog(@"The answer changed to %@", x);
}];
// Derived value
RACSignal *plusOne = [[self.connection
signalForKey:@"the-answer"]
map:^(NSNumber *answer) {
return @(answer.unsignedIntegerValue + 1);
}];
[self.connection rac_liftSelector:@selector(writeObject:forKey:) withObjects:plusOne, @"the-answer-plus-1"];
// Trimming
RACSignal *trimmed = [[[self.connection
signalForKey:@"some-array"]
filter:^ BOOL (NSArray *array) {
return array.count > 50;
}]
map:^(NSArray *array) {
return [array subarrayWithRange:NSMakeRange(array.count - 10, 10)];
}];
[self.connection rac_liftSelector:@selector(writeObject:forKey:) withObjects:trimmed, @"some-array"];
// Validation
RACSignal *valid = [RACAble(self.someProperty) filter:^(id value) {
return ![value isNotCrazy];
}]
[self.connection rac_liftSelector:@selector(writeObject:forKey:) withObjects:valid, @"some-stuff"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment