2013-04-02 5 views
7

Sto utilizzando UAGitHubEngine per accedere all'API di GitHub. Voglio scrivere un'app reattiva funzionale per recuperare alcuni dati. Sto facendo affidamento sul codice here per impostare una richiesta di rete asincrona. Quello che sto cercando è l'id di squadra di una squadra chiamata "Generale". Posso fare il filtraggio/stampa parte OK:Utilizzare RACCommand con operazione di rete asincrona

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

Ma impostazione del comando è un mistero per me:

self.gitHubCommand = [RACCommand command]; 

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

    return signal; 
}]; 

Come faccio a impostare il blocco del segnale per restituire un segnale che spinge un evento quando ritorna una chiamata di rete asincrona?

risposta

4

La risposta era in RACReplaySubject, che AFNetworking uses avvolge le sue richieste asincrone.

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

Dal addSignalBlock: restituisce un segnale di segnali, abbiamo bisogno di iscriversi al prossimo segnale che emette.

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

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

Infine, il blocco addSignalBlock: viene eseguito solo quando viene eseguito il comando, che sono riuscito con il seguente:

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