3

I'm new to ReactiveCocoa and would like to use it as a replacement for KVO on some NSManagedObjects in a Swift 2 project.

Most of the examples I found online use RACObserve(), which has been removed(?) in RAC 3. The Changelog states, that the new versions de-emphesize KVO and I should move to PropertyTypes.

Is there any way that I can use Reactive Cocoa 3 and 4 on an existing NSManagedObject in a similar way to what could be done with RACObserve()?

Tim Bodeit
  • 9,673
  • 3
  • 27
  • 57
  • RACObserve is a macro, so you can't use it. But you can just use the underlying method rac_valuesForKeyPath in Swift. – Cosyn Oct 17 '15 at 11:32

1 Answers1

6

ReactiveCocoa 3.0 documentation points to the DynamicProperty

The DynamicProperty type can be used to bridge to Objective-C APIs that require Key-Value Coding (KVC) or Key-Value Observing (KVO), like NSOperation. Note that most AppKit and UIKit properties do not support KVO, so their changes should be observed through other mechanisms. MutableProperty should be preferred over dynamic properties whenever possible!

So you need to make an DynamicProperty object and use it's signalProducer. Something like this:

DynamicProperty(object: managedObject, keyPath: "attribute").producer

P. S. ReactiveCocoa source code has awesome inline documentation. Checkout out it for more information.

skyylex
  • 855
  • 1
  • 10
  • 24
  • Thanks for the usage example! I did find the recommendation of DynamicProperty, but misinterpreted how to use it. I thought it was a wrapper type like `MutableProperty`, but with KVC and KVO compliance and that using it would require me to change the interface of the NSManagedObject subclass.Your answer cleared up things a lot. – Tim Bodeit Oct 19 '15 at 09:48