Reputation: 3114
I have the following classes and methods:
Class A
- (RACSignal *)createX
{
NSDictionary *parameters = @{};
return [[[[HTTPClient sharedClient] rac_POST:@"X/" parameters:parameters]
map:^id(OVCResponse *response) {
[self logResponse:response];
return response.result;
}] catch:^RACSignal *(NSError *error) {
return [RACSignal error:[self handleError:error]];
}];
}
Class B
- (void)requestData
{
[[self.myClassA createX]
subscribeNext:^(NSArray *results) {
DDLogDebug(@"response : %@", results);
}
error:^(NSError *error) {
[self.dataManager sendError:error];
}];
}
Class C
- (void)retrieveData
{
[self.myClassB requestData];
}
What is the best way to design requestData
in Class B
such that the results
array can be accessed in Class C
.
i.e.
Should I forward the Array some way using [array rac_sequence]
,
should I create a new signal inside requestData, should requestData
return a RACSignal
instead of void
?
Any help or guidance would be greatly appreciated. Thanks.
Upvotes: 1
Views: 84
Reputation: 1266
I'm not entirely clear on your use case here but I think you are mixing paradigms. RAC stuff is always asynchronous so in order to access the result of your network request synchronously you will have to store it in some way.
You could bind the result to a property on ClassB or you could use RACCommand
, something like:
[[RACCommand alloc] initWithEnabled:RACObserve(self, valid) signalBlock:^RACSignal *(id input) {
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//make network call
//send responseObject to subscriber
[subscriber sendNext:responseObject];
//[subscriber sendError:#NSError#] //send error if something went wrong
[subscriber sendCompleted];
return nil;
}] materialize];
}];
You can then subscribe to the executionSignals
of the RACCommand
which streams a RACSignal
for each execution of the command which you have control over in the block described above.
So I think your options are:
RACCommand
pattern, look into it a bit moreRACSignal
as you describe upon calling the methodreplay()
or replayLast()
here as you could then store a reference to the RACSignal
and subscribe to it for access to its last valueUpvotes: 0
Reputation: 524
I believe you want to use doNext
instead of subscribeNext
in Class B.
Upvotes: 1