Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI signals vs then #2671

Closed
andrzejtt opened this issue Jan 20, 2016 · 5 comments
Closed

UI signals vs then #2671

andrzejtt opened this issue Jan 20, 2016 · 5 comments

Comments

@andrzejtt
Copy link

Why does below then construct work with custom signals but does not with UI signals (then closure is not even called there)? My goal is to wait for button tap before processing text from a text field.

#if true
    // Why does this not work?

    button.rac_signalForControlEvents(.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
@iv-mexx
Copy link
Contributor

iv-mexx commented Jan 20, 2016

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. I guess you customSignal() creates a signal that completes immediately or after some time, thats why this works.

Is it correct that you want to set the text of self.label to the value of self.textField on each button tap?

I'm not really an expert, but I would try to use sample for that (rough, untested):

self.textField.rac_textSignal().sample(self.button.rac_signalForControlEvents(.TouchUpInside))

@andrzejtt
Copy link
Author

Thanks! That clears up the matter. Looks like I still do not entirely get how the Signals work :)

As for "Is it correct that you want to set the text of self.label to the value of self.textField on each button tap?" - no, I wanted to change label text to text in text field in real time only after button was tapped before. Do you know of any RCA setup that specifies that?

@iv-mexx
Copy link
Contributor

iv-mexx commented Jan 20, 2016

Ah ok, and once the button you want to keep updating the text label indefinitely, even if the button is tapped again?

My first idea was to use skipUntil, but it seems this operator is not implemented in terms of another signal in RAC. Theres only - (instancetype)skipUntilBlock:(BOOL (^)(id x))predicate; in RACStream or public func skipWhile(predicate: Value -> Bool) -> Signal<Value, Error> in Signal.

I would try to use those, but you'll need to implement the predicate block yourself instead of just doing skipUntil(button.rac_signal.....)

Maybe theres a better way that I don't know of though.

@iv-mexx
Copy link
Contributor

iv-mexx commented Jan 20, 2016

Oh, seems like I was wrong (I was looking at my outdated local repo instead of the current one). Turns out, there is public func skipUntil(trigger: Signal<(), NoError>) -> Signal<Value, Error> implemented in Signal.

However, it seems like you are currently using RACSignal from the old ObjC API, you would need to convert those to Signal to use the operators from the Swift API.

You could also take a look at Rex for UIKit Bindings for the Swift API

@andrzejtt
Copy link
Author

Thanks again. I'll look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants