Ríomhaire
Ríomhaire

Reputation: 3114

ReactiveCoca Design Pattern for Forwarding / Chaining Signals

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

Answers (2)

jwswart
jwswart

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 more
  • Bind result of network call to a property for synchronous access
  • Return a RACSignal as you describe upon calling the method
  • Possibly look into replay() or replayLast() here as you could then store a reference to the RACSignal and subscribe to it for access to its last value

Upvotes: 0

bdjett
bdjett

Reputation: 524

I believe you want to use doNext instead of subscribeNext in Class B.

Upvotes: 1

Related Questions