How can I subscribe to the completion of a command's execution signals without a nested subscription?

后端 未结 2 1079
清酒与你
清酒与你 2020-12-29 17:56

I tried the following without success. The equivalent using -subscribeNext: works as expected.

// A
[[_viewModel.loginCommand.executionSignals f         


        
2条回答
  •  醉话见心
    2020-12-29 18:22

    The -flatten operator returns a signal that completes only when all of the inner signals have completed, which requires the outer signal to complete as well. The same is true of -concat. Because of this, once you apply either operator, the resulting signal has no representation of individual completion, only the final aggregate completion.

    Alternative to nested subscriptions, you could transform the inner signals so that they send a value that signifies completion. One way of doing this is with -materialize:

    [[[_viewModel.loginCommand.executionSignals
        map:^(RACSignal *loginSignal) {
            // Using -ignoreValues ensures only the completion event is sent.
            return [[loginSignal ignoreValues] materialize];
        }]
        concat]
        subscribeNext:^(RACEvent *event) {
            NSLog(@"Completed: %@", event);
        }];
    

    Note that I used -concat instead of -flatten, since it matches the semantics of RACCommand's default serial execution. They ultimately do the same in this case, -flatten degenerates to the behavior of -concat because the command only executes signals one at a time.

    Using -materialize isn't the only way to do this, it just happens to send a value that represents completion, but that could be any value that you find appropriately significant for your use case.

提交回复
热议问题