iosswiftcocoa-touchreactive-cocoa-4

Reactive Cocoa - then on custom signal vs UI signal


I'm starting to work with Reactive Cocoa so I've written a little test to see whether I understand then construct. The goal was to output text from a text field to the label only after some signal is sent. I've tried rac_signalForControlEvents(UIControlEvents.TouchUpInside) and VERY simple custom signal. Former does not even call then closure while the latter does work as intended. What is the problem here?

func someSetupFunction()
{
#if true
    // Why does this doesn't work?

    button.rac_signalForControlEvents(UIControlEvents.TouchUpInside).then
        {
            let strongSelf = weakSelf
            return strongSelf?.textField.rac_textSignal()
        }.subscribeNext
        { object in
            let strongSelf = weakSelf
            strongSelf?.label.text = object as! String! + " - 0"
        }
#else
    // ... but this does?

    customSignal().then
        {
            let strongSelf = weakSelf
            return strongSelf?.textField.rac_textSignal()
        }.subscribeNext
        { object in
            let strongSelf = weakSelf
            strongSelf?.label.text = object as! String! + " - 1"
        }
#endif
}

func customSignal() -> RACSignal
{
    return RACSignal.createSignal
        { subscriber in
            subscriber.sendNext(nil)
            subscriber.sendCompleted()
            return nil
        }
}

Solution

  • From the docs for then:

    Ignores all nexts from the receiver, waits for the receiver to complete, then subscribes to a new signal.

    button.rac_signalForControlEvents does not complete (at least not on a button tap), thats why nothing happens.

    Original answer by iv-mexx can be found at https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2671