【问题标题】:Two way binding with custom signal with Reactive Cocoa not working as expected与 Reactive Cocoa 的自定义信号的两种方式绑定无法按预期工作
【发布时间】:2014-09-08 12:25:04
【问题描述】:

我有自定义 UITextField 具有 NSDecimalNumber 值。我想以两种方式将它绑定到我的模型的 price 键路径。

问题是价格字段是 brutto 价格,并且取决于我的自定义构建信号(价格、税收、折扣等信号的总和)

所以我尝试在下面的代码中创建RACChannelTerminal,但我仍然得到无限循环的更新。 TextField 正在更新价格,价格正在更新 TextField。

谁能给我一个提示如何使这种双向数据绑定工作?

// Connecting two way PRICE
RACChannel *channelForProductPrice = [[RACChannel alloc] init];
[channelForProductPrice.followingTerminal subscribeNext:^(NSDecimalNumber *newValue) {
    self.product.price = [Price priceWithDecimalNumber:newValue];
}];
[[[[self.product rac_signalForPrice] takeUntil:[channelForProductPrice.followingTerminal
                                                ignoreValues]] map:^NSDecimalNumber *(Price *nettoPrice) {
    return [self.product calculatePriceWithTax:YES withDiscount:NO unit:YES rounded:YES].value;
}] subscribe:channelForProductPrice.followingTerminal];


RACChannel *channelForBruttoTextField = [[RACChannel alloc] init];
[channelForBruttoTextField.followingTerminal subscribeNext:^(NSDecimalNumber *value) {
    self.bruttoPriceTextField.value = value;
}];
[[[[self.bruttoPriceTextField rac_signalForValueChange] takeUntil:[channelForBruttoTextField.followingTerminal ignoreValues]] map:^id(id value) {
    NSDecimalNumber *newPrice = [value decimalNumberByDividingBy:self.product.tax.taxRatePlusOneAsPercentage];
    return newPrice;
}] subscribe:channelForBruttoTextField.followingTerminal];


[channelForProductPrice.leadingTerminal subscribe:channelForBruttoTextField.leadingTerminal];
[channelForBruttoTextField.leadingTerminal subscribe:channelForProductPrice.leadingTerminal];

【问题讨论】:

    标签: ios objective-c reactive-cocoa


    【解决方案1】:

    看起来你正在管道 followingTerminal -(map)-> price -> followingTerminal,没有什么可以打破这个循环。我不认为这真的是你的意思。

    您可能最好设置一些RACSignals 而不是RACChannels,使用distinctUntilChanged 或类似的东西来打破循环,并酌情使用skip: 来阻止错误值在您连接时流动。目前,我还不清楚通过此的确切信息流。

    【讨论】:

    • 是的,我终于用distinctUntilChanged了。我读到他们想从 ReactiveCocoa 3.0 中删除 RACChannel,因为它很难检测到周期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2015-11-16
    • 2021-04-23
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多