2013-04-02 105 views
7

我使用UAGitHubEngine來訪問GitHub的API。我想寫一個功能反應型應用程序來獲取一些數據。我依靠代碼here來設置異步網絡請求。我正在尋找的是一些名爲「General」的團隊的團隊ID。我可以做過濾/印刷部分OK:使用RACCommand與異步網絡操作

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

如何設置了信號塊返回推的一個信號某些異步網絡呼叫返回時的事件?

回答

4

答案在RACReplaySubject,其中AFNetworking 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]];