سؤال

I want to do various tasks when self.personsArray is updated as follows:

- (void)viewDidAppear:(BOOL)animated
{
    @weakify(self)

    [RACObserve(self, personsArray) subscribeNext:^(NSArray *personsArray) {
        @strongify(self)

        // update a set of views

        [self.view setNeedsUpdateConstraints];
        [self.view setNeedsLayout];
    }];

    [RACObserve(self, personsArray) subscribeNext:^(NSArray *personsArray) {
        @strongify(self)

        // update a second set of views

        [self.view setNeedsUpdateConstraints];
        [self.view setNeedsLayout];
    }];

    [RACObserve(self, personsArray) subscribeNext:^(NSArray *personsArray) {
        @strongify(self)

        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];
    }];
}

I prefer separating the work in distinct blocks because it provides a logical separation. I'd like to guarantee the the last subscription message (the message that executes the animation block) is sent last. How can I do this?

It seems to me that what I'm looking for might be chaining (rather than multiple independent sets of signals as subscriptions, as I've done in this example), but I can't quite connect the dots.

هل كانت مفيدة؟

المحلول

Why don't you just update different parts like this:

- (void)viewDidAppear:(BOOL)animated
{
    @weakify(self)

    [RACObserve(self, personsArray) subscribeNext:^(NSArray *personsArray) {
        @strongify(self)

        // update a set of views
        [self updateFirstSetOfView];
        [self updateSecondSetOfView];
        [self doSomethingElse];

        // Finally
        [UIView animateWithDuration:0.4 animations:^{
            [self.view layoutIfNeeded];
        }];

    }];
}

You could achieve the same with -doNext: if you prefer the chaining.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top