ReactiveCocoa subscribe to completed event of flattenmaped signal

250 Views Asked by At

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()
            }
2

There are 2 best solutions below

1
Michał Ciuba On BEST ANSWER

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.

0
Charles Maria On

The flattenMap can be seen as turning the whole signal into a concat of empty signals, that is completed won't be sent until every empty signal completes (the signal being flattenMapped completes)