未调用ReactiveCocoa flattenMap

时间:2014-12-25 21:49:34

标签: ios objective-c functional-programming reactive-cocoa frp

我有两个数据源,它们包含联系人和用户的不同数组,以及一个聚合的Invitee数据源,用于组合并保持对联系人和用户结果的引用:

AddressBookDataSource:

- (RACSignal *)getContacts {
    return [[[[self getContactsSignal] flattenMap:^RACStream *(NSArray *contacts) {
        return contacts.rac_sequence.signal;
    }]
    map:^id(APContact *contact) {
        return [[Contact alloc] initWithAPContact:contact];
    }] collect];;
}

- (RACSignal*)getContactsSignal {
    APAddressBook *addressBook = [[APAddressBook alloc] init];
    addressBook.fieldsMask = APContactFieldFirstName | APContactFieldCompositeName | APContactFieldPhoto;
    RACSignal *addressBookSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [addressBook loadContacts:^(NSArray *contacts, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            } else {
                [subscriber sendNext:contacts];
            }
        }];
        return nil;
    }];
    return addressBookSignal;
}

ParseDataSource:

- (RACSignal *)getUsers {
    return [[[[[self getUsersSignal] flattenMap:^RACStream *(NSArray *users) {
        return users.rac_sequence.signal;
    }] filter:^BOOL(User *user) {
        return ![user.username isEqualToString:[User currentUser].username];
    }] map:^id(User *user) {
        return user;
    }] collect];
}

- (RACSignal*)getUsersSignal {
    RACSignal *getUsersSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        PFQuery *userQuery = [User query];
        [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            } else {
                [subscriber sendNext:objects];
            }
        }];
        return nil;
    }];

    return getUsersSignal;
}

InviteeDataSource:

- (RACSignal*)getPotentialInvitees {
    ParseDataSource *parseDataSource = [[ParseDataSource alloc] init];
    AddressBookDataSource *addressBookDataSource = [[AddressBookDataSource alloc] init];

    return [[RACSignal concat:@[
                              [parseDataSource getUsers],
                              [addressBookDataSource getContacts]
                              ]]
            flattenMap:^RACSignal *(RACTuple *tuple) {
                RACTupleUnpack(NSArray *users, NSArray *contacts) = tuple;
                _contactSection.contacts = contacts;
                _userSection.users = users;
                return [RACSignal empty];
            }];
}

问题是flattenMap块永远不会被调用,这意味着后续订阅者永远不会调用他们的subscribeNext块。

帮助?

1 个答案:

答案 0 :(得分:0)

感谢来自@jspahrsummers的Twitter回复,有一些事情使得这不符合我的意愿,但问题的根源似乎是我没有打电话给-sendCompleted-getUsersSignal-getContactsSignal

中的订阅者