Use RACCommand with Asynchronous Network Operation

I am using UAGitHubEngine to access the GitHub API. I want to write a functional reactive application to get some data. I rely on the code here to configure an asynchronous network request. What I'm looking for is the team identifier of some team called General. I can do the filtering / printing part OK:

 [[self.gitHubSignal filter:^BOOL(NSDictionary *team) { NSString *teamName = [team valueForKey:@"name"]; return [teamName isEqualToString:@"General"]; }] subscribeNext:^(NSDictionary *team) { NSInteger teamID = [[team valueForKey:@"id"] intValue]; NSLog(@"Team ID: %lu", teamID); }]; 

But setting up the command is mysterious to me:

 self.gitHubCommand = [RACCommand command]; self.gitHubSignal = [self.gitHubCommand addSignalBlock:^RACSignal *(id value) { RACSignal *signal = ??? return signal; }]; 

How to configure a signal unit to return a signal that triggers an event when an asynchronous network call returns?

+7
source share
1 answer

The answer was in RACReplaySubject , which AFNetworking uses to migrate its asynchronous requests.

 self.gitHubCommand = [RACCommand command]; self.gitHubSignals = [self.gitHubCommand addSignalBlock:^RACSignal *(id value) { RACReplaySubject *subject = [RACReplaySubject subject]; [engine teamsInOrganization:kOrganizationName withSuccess:^(id result) { for (NSDictionary *team in result) { [subject sendNext:team]; } [subject sendCompleted]; } failure:^(NSError *error) { [subject sendError:error]; }]; return subject; }]; 

Since addSignalBlock: returns a signal of signals, we need to subscribe to the next signal that it emits.

 [self.gitHubSignals subscribeNext:^(id signal) { [signal subscribeNext:^(NSDictionary *team) { NSInteger teamID = [[team valueForKey:@"id"] intValue]; NSLog(@"Team ID: %lu", teamID); }]; }]; 

Finally, the addSignalBlock: block addSignalBlock: not executed until the command that I succeeded is executed:

 [self.gitHubCommand execute:[NSNull null]]; 
+4
source

All Articles