将RAC命令与异步网络操作一起使用

时间:2013-04-02 17:58:57

标签: ios objective-c reactive-cocoa raccommand

我正在使用UAGitHubEngine来访问GitHub的API。我想写一个功能性的被动应用程序来获取一些数据。我依靠代码here来设置异步网络请求。我正在寻找的是一些名为“General”的团队的团队ID。我可以做过滤/打印部分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);
}];

但是设置命令对我来说是一个谜:

self.gitHubCommand = [RACCommand command];

self.gitHubSignal = [self.gitHubCommand addSignalBlock:^RACSignal *(id value) {
    RACSignal *signal = ???

    return signal;
}];

当某些异步网络调用返回时,如何设置信号块以返回推送事件的信号?

1 个答案:

答案 0 :(得分:4)

答案在RACReplaySubjectAFNetworking uses包装其异步请求。

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;
}];

由于addSignalBlock:返回信号信号,我们需要订阅它发出的下一个信号。

[self.gitHubSignals subscribeNext:^(id signal) {
    [signal subscribeNext:^(NSDictionary *team) {
        NSInteger teamID = [[team valueForKey:@"id"] intValue];

        NSLog(@"Team ID: %lu", teamID);
    }];
}];

最后,在执行命令之前不执行addSignalBlock:块,我使用以下命令管理:

[self.gitHubCommand execute:[NSNull null]];