-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[RAC-3.0] Question 2-Way Binding Example? #1986
Comments
I would take a look at ReactiveCocoa/EditableProperty that @jspahrsummers recently started to enable multi-way binding. |
I assume that my code contains extra code, but I have implemented two-way binding for private let _property: MutableProperty<String>
var property: PropertyOf<String> {
return PropertyOf(_property)
} _property = MutableProperty<String>("")
_property <~ userDefaults.rac_signalForKey(SomeKey)
updatePropertyAction = Action<String, String, NoError> {
userDefaults.setObject($0, forKey: UserDefaultsKeywordsKey)
userDefaults.synchronize()
return SignalProducer.empty
} |
how does |
The propertyTextView.rac_textSignal().toSignalProducer()
|> ignoreError
|> map { $0 as? String ?? "" }
|> map { property in
return self.viewModel.updatePropertyAction.apply(property) |> ignoreError
}
|> flatten(FlattenStrategy.Concat)
|> start() |
It would be great if @jspahrsummers comments about the direction of my thoughts. |
@larryonoff Seems like that could cause a recursive notification if the thing observing the property is also the thing modifying user defaults. |
@jspahrsummers I agree. This is why extension NSUserDefaults {
/// Sends value of key when the value is changed
func rex_signalForKey(key: String) -> Signal<AnyObject?, NoError> {
let (signal, observer) = Signal<AnyObject?, NoError>.pipe()
// send initial value
let initial: AnyObject? = self.objectForKey(key)
sendNext(observer, initial)
// observe other values
let defaultsDidChange = NSNotificationCenter.defaultCenter().rac_notifications(name: NSUserDefaultsDidChangeNotification, object: self)
defaultsDidChange.observe(next: { notification in
let value: AnyObject? = self.objectForKey(key)
sendNext(observer, value)
}, completed: {
sendCompleted(observer)
})
return signal
|> skipRepeats { a, b in
if let a = a as? NSObject, b = b as? NSObject where a.isEqual(b) {
return true
} else {
return false
}
}
}
} |
In any case, it looks like the question here has been answered. |
I'm assuming 2 way binding is something like this?
The text was updated successfully, but these errors were encountered: