Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Accomplish this w/ ReactiveCocoa

I'm building a feature where users of my app can find their Facebook friends and add them in the app. There are three steps which I have to do:

  1. Get the currently connected users
  2. Get the Facebook users
  3. Get the Application Users (this is dependent on step 2)

Once all of these complete I then need to combine/reduce the three resulting arrays into a final array.

I have created three functions that all return RACSignal

getUsersWithFacebookIds, getConnectedUsers, and getFacebookUsers

I'm not sure how to wire all of this using ReactiveCocoa.

enter image description here

like image 760
Kyle Decot Avatar asked Mar 17 '23 09:03

Kyle Decot


1 Answers

The Once All Are Done Do Something With All, can be achieve with:

[[RACSignal combineLatest:@[connectUsersSignal,facebookUsersSignal,applicationUsersSignal]] subscribeNext:^(RACTuple *users) {
    NSArray *connectedUsers = [users first];
    NSArray *facebookUsers = [users second];
    NSArray *applicationUsers = [users third];

}];

The other piece missing is how you make the applicationUsersSignal dependent on the facbookUsersSignal. Which could be done like this:

- (RACSignal *)applicationUsersSignalWithFacebookUsersSignal:(RACSignal *)fbSignal
{
    return [fbSignal flattenMap:^RACStream *(NSArray *facebookUsers) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            // Do what you have to do with the facebookUsers
            return nil;
        }];
    }];
}

Just to add a bit more to the answer. I am assuming those are cold signals, (signals that haven't started yet aka haven't been subscribed). So the idea of using combineLatest: is that you want to capture the point where each and every single signal as send at least one next, after that you subscribe to it so it can begin. Finally you can get their values from a RACTuple.


I re read your question and you want them to come all in a single Array:

[[[RACSignal combineLatest:@[connectUsersSignal,facebookUsersSignal,applicationUsersSignal]] map:^id(RACTuple *allArrays) {
    return [allArrays.rac_sequence foldLeftWithStart:[NSMutableArray array] reduce:^id(id accumulator, id value) {
        [accumulator addObjectsFromArray:value];
        return accumulator;
    }];
}] subscribeNext:^(NSArray *allUsers) {
    // Do Something
}];
like image 69
Rui Peres Avatar answered Mar 29 '23 20:03

Rui Peres