简体   繁体   中英

ReactiveCocoa subscribe to completed event of flattenmaped signal

This is my code snippet. The issue is it doesn't reach subscribeCompleted block. It supposed to immediately complete as I return empty signal inside flattenmap block. Isn't it?

RACObserve(self.object, "mobile").skip(2).doNext { (_) -> Void in
                self.tabBarController?.showHud("Updating Profile")
            }.flattenMap { (object) -> RACStream! in
                return RACSignal.empty()
            }.subscribeCompleted { () -> Void in
                log.error("Completed")
                self.tabBarController?.hideHud()
            }

The signal returned by flattenMap will complete only when the "source" signal completes. In your case you apply flattenMap operator to the following signal:

RACObserve(self.object, "mobile").skip(2)

The signal returned by RACObserve completes only when the object being observed is deallocated. Depending on what you want to achieve, you can use some operators to transform the signal and get another one that will complete earlier.

For example, you can use filter and take so that the resulting signal completes after sending its first value matching some conditions:

RACObserve(self.object, "mobile").skip(2).doNext { (_) -> Void in
                    self.tabBarController?.showHud("Updating Profile")
}.filter {
//some filtering for the value of self.object.mobile 
     return $0.checkSomeConditions() 
}.take(1)
.subscribeCompleted { () -> Void in
        log.error("Completed")
        self.tabBarController?.hideHud()
}

Note that you don't even need flattenMap at all. The signal will simply complete because of take operator.

可以将flattenMap视为将整个信号转换为空信号的连接,直到每个空信号完成(被flattenMapped的信号完成),才会发送完成信号

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM