zakdances
zakdances

Reputation: 23685

How can I bind two RACChannelTerminals?

I have two NSTextViews that I want synced together. I'm using ReactiveCocoa to generate RACChannelTerminals from their cocoa bindings.

RACChannelTerminal *terminal1 = [textView1 rac_channelToBinding:@"attributedString" options:@{ NSContinuouslyUpdatesValueBindingOption: @(YES) }];
RACChannelTerminal *terminal2 = [textView2 rac_channelToBinding:@"attributedString" options:@{ NSContinuouslyUpdatesValueBindingOption: @(YES) }];

So naturally I thought the next step was just to make a RACChannel, then plug in both terminals to it.

RACChannel *channel = [RACChannel new];
channel.leadingTerminal = terminal1;
channel.followingTerminal = terminal2;

But then the compiler says nope: Assignment to read only property. It seems like this should be straight forward, so what am I doing wrong here? How do I create a RACChannel-like binding with my own terminals?

Upvotes: 3

Views: 1540

Answers (1)

Dave Lee
Dave Lee

Reputation: 6489

The two RACChannelTerminals need to be subscribed to each other.

[terminal1 subscribe:terminal2];
[terminal2 subscribe:terminal1];

The initial value will be ignored, syncing won't happen until new text is entered.

EDIT:

To have them initially synced, I did this:

NSString *initialText = textView1.stringValue;

// ... setup channel terminals

[[terminal1 startWith:initialText] subscribe:terminal2];
[[terminal2 startWith:initialText] subscribe:terminal1];

Upvotes: 10

Related Questions