Can / How do I replace KVO with RC3?

I am trying to port an objc application that uses Facebook KVOController for Swift. I was asked to consider RC3 as an alternative and faster approach. I have read several blogs and I am invited to try. But most documents and blogs seem to concentrate on sockets and timers as examples. So I have two simple questions right now:

1) For an objc fragment of type:

 [self.KVOController observe: self.site keyPath: @"programs" options: NSKeyValueObservingOptionInitial block:^(id observer, id object, NSDictionary *change) { [self.tableView reloadData]; }]; 

What is an easy way to rewrite this using the RC3 API? And what is the RC3 equivalent for self.KVOController unobserve: self.site , which happens when this view is unloaded?

2) He recommended using Carthage to capture RC3 code. Can I safely mix Cartfile with a project that uses cocoa pods already for Alamofire and SwiftyJSON ?

+7
swift key-value-observing reactive-cocoa-3 carthage reactive-cocoa
source share
2 answers

Disclaimer :) I am also new to the whole concept of ReactiveCocoa and especially to RAC 3. I play with a new knowledge base, so my solution cannot be the intended use of the API. I would be happy to receive feedback from others and find out the right way.

Here is what I could come up with for your first question: You can define the property that you want to observe as MutableProperty. The following is the simplest example using the String property. Suppose you have a view model, it has a String property, and you want to watch this in your view controller object.

ViewModel.swift

 class ViewModel { var testProp = MutableProperty<String>("") } 

ViewController.swift

 class ViewController: UIViewController { private let viewModel = ViewModel() override func viewDidLoad() { super.viewDidLoad() let testPropObserver = viewModel.testProp.producer testPropObserver.start(next: {value in println("new value \(value)") }) viewModel.testProp.value = "you should see this in your log" } } 
+8
source share

On the first question, I can only guide you in the right direction. Take a look at the log to 3.0 change that describes the transition from RACObserve () to PropertyTypes. This is a little more clear, and it offers type safety. Here is a source, which, in my opinion, is the best choice for figuring out how to implement it. You can also take a look at their test cases to see if they built any tests for the PropertyType protocol and simulate how they configured it.

And yes, you will not have problems mixing Carthage and CocoaPods in one project, since Carthage should be minimally intrusive. As long as you follow the instructions on your website, you should be fine.

+2
source share

All Articles