Search code examples
iosreactive-cocoafrpreactive-cocoa-3

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

Solution

  • 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.