Reputation: 64428
I don't understand where the RACSignal object comes from in the following example in the ReactiveCocoa github documentation.
Under Parallelizing independent work, it first shows the classic objective-c version:
__block NSArray *databaseObjects;
__block NSArray *fileContents;
NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
NSBlockOperation *databaseOperation = [NSBlockOperation blockOperationWithBlock:^{
databaseObjects = [databaseClient fetchObjectsMatchingPredicate:predicate];
}];
Clearly, [databaseClient fetchObjectsMatchingPredicate:predicate]
returns an array. But, in the reactive version:
RACSignal *databaseSignal = [[databaseClient
fetchObjectsMatchingPredicate:predicate] // <== this should also be an array
subscribeOn:[RACScheduler scheduler]]; // ,<== subscribeOn: is a RACSignal Method only
... it looks like the same method is supposed to return not an array (which would have a rac_sequeance attribute) but a RACSignal.
In this example, is it meant to be read that `databaseClient' class in the RAC example is actually a subclass of RACSignal? Or, is a signal being created in some way not shown?
Currently running MacOS 10.9.3 with Reactive Cocoa 2.3. Documentation is from the ReactiveCocoa 3.0 branch.
Upvotes: 2
Views: 178
Reputation: 6308
It's fairly idiomatic in ReactiveCocoa code to name a method that returns a signal in a similar fashion to a method that would return the value directly. In other words, even though the method that returns the signal doesn't actually perform the action directly (when the method is executed), it's given a method name that suggests that it does.
For example, a method that queries a database and returns the result of that query directly might be named like this:
- (DBResult *)queryDatabase:(DBQuery *)query;
Whereas a RAC-ified version of this method would probably be named like this:
- (RACSignal *)queryDatabase:(DBQuery *)query;
It might seem a bit unintuitive at first, because technically that method doesn't query the database – instead, it returns a signal that results in an action to query the database when the signal is subscribed to – but that's the convention. You don't have to observe the convention in your own code, but a perusal of the OctoKit source code will show you that it's not uncommon.
In this example, is it meant to be read that
databaseClient
class in the RAC example is actually a subclass of RACSignal? Or, is a signal being created in some way not shown?
In this example, databaseClient
is not a subclass of RACSignal. It is just an object that responds to the -fetchObjectsMatchingPredicate:
message, and returns a RACSignal from the corresponding method. Hypothetically, the object's class might look something like:
@interface MyDatabaseClient : NSObject
- (RACSignal *)fetchObjectsMatchingPredicate:(NSPredicate *)predicate;
@end
Upvotes: 2