RACChannel可以被看成是一個(gè)由兩個(gè)并行工作的signal組成的雙向連接。例如,當(dāng)連接一個(gè)view和一個(gè)model時(shí)有如下模型:
Model View
leadingTerminal ------> followingTerminal
leadingTerminal <------ followingTerminal
Model的初始值以及后面值的改變都會(huì)發(fā)送到leadingTerminal,由followingTerminal的訂閱者所接收。
同樣,不論何時(shí)view的值發(fā)生了改變,修改后的值會(huì)發(fā)送到followingTerminal,leadingTerminal中的model會(huì)收到。但是,view的初始值不回被收到。
RACChannelTo宏
RACChannelTo()作為右值表達(dá)式時(shí),它返回一個(gè)RACChannelTerminal,可以用來觀測(cè)特定屬性的改變。如果作為左值,那么右值必須是一個(gè)RACChannelTerminal值。兩個(gè)RACChannelTerminal互相訂閱,左邊的屬性值會(huì)立刻發(fā)送到右邊的信號(hào),然后接下來值的改變都會(huì)發(fā)給對(duì)方。
在使用兩個(gè)textField進(jìn)行雙向綁定測(cè)試時(shí)發(fā)現(xiàn)了這么一個(gè)現(xiàn)象:
self.valueTextField.rac_newTextChannelsends values when you type in the text field, but not when you change the text in the text field from code.
RACChannelTo(self.uiTextField, text)sends values when you change the text in the text field from code, but not when you type in the text field.
RACKVOChannel繼承RACChannel,通過下面方法初始化:
``` - (id)initWithTarget:(NSObject *)target keyPath:(NSString *)keyPath nilValue:(id)nilValue;```
當(dāng)前key path的值以及后續(xù)的KVO通知都是發(fā)送給channel的following terminal的訂閱者。