Group Group Group Group Group Group Group Group Group Forums

Getting Started With RxSwift and RxCocoa

Learn how to use RxSwift and RxCocoa to write applications that can react to changes in your underlying data without you telling it to do so.

This is a companion discussion topic for the original entry at

Had some RxSwift compiling errors and fixed by updating RxSwift and RxCocoa to version 3.0.0-beta.2

Awesome Tutorial!!

1 Like

Hi, Thanks for the great tutorial.

But I got build errors on Xcode 8.0 for the starter project, even after has used convert tool. Maybe I miss something ?

For anyone who hasn’t used CocoaPods but has it installed, open the Podfile and change the following:


target 'Chocotastic' do
    pod 'RxSwift', '~> 3.0.0.alpha.1'
    pod 'RxCocoa', '~> 3.0.0.alpha.1'


target 'Chocotastic' do
    pod 'RxSwift', '~> 3.0.0.beta.2'
    pod 'RxCocoa', '~> 3.0.0.beta.2'

Then navigate to the project directory in terminal and run pod install

Are your problems fixed now? If they are still happening and you have tried the RxSwift/RxCocoa updates recommended, let us know what the error output is.

Hey all! Thanks for the heads up - I’ve been on the road with intermittent reception the last couple days.

I’ve updated the sample code to use 3.0.0-beta1, which builds and runs just fine on Xcode 8.0, so y’all should be good to go.

1 Like

Yea, It worked now after downloading again the start project updated by the author.

1 Like

One of the more solid guides I’ve seen when it comes to explaining Rx, but needlessly complex iOS code bogs it down. I found myself saying “wait… what the hell does that do?” quite a few times to things that had nothing to do with Rx. A tableView displaying [String] segueing to a page that checks whether the String contains a valid phone number would have been more effective learning example than one with 1500 lines of code that add nothing to the example.

Hi I liked your tutorial , but can you explain (add to tutorial) how to work with observable if for example in card view controller with chocolates you want to change amount of some chocolates (remove,add more) and go back to previously view controller.

Hi Nathan - I actually purposely tried to isolate the code that validated the credit card information and expiration date (which I believe is what you mean rather than phone number, since that’s not actually included in the billing info here) in another file.

Since it wasn’t super-relevant to the actual implementation of Rx, which is what this tutorial was concentrating on, the idea was to make it simpler for the developer to focus on the result of those operations rather than what those operations themselves were doing by abstracting them out.

Obviously that won’t always be the case for everyone (clearly it wasn’t for you), since everyone reads code differently, but this was the reason I took the approach that I did, for what it’s worth.

Hi Basconje - So if I understand your question correctly, the question is what happens if you change the number of chocolates in the cart when adding the the credit card. The tutorial isn’t really set up to do this, but you can see the effect if, from the CartViewController, you remove all the chocolates by hitting the big red Reset button.

In the non-reactive version of the tutorial, the count of chocolates in the cart is re-calculated on viewWillAppear(_:) to display the updated value. But once the app is using Observables, that call is removed. You’ll notice the count is still re-calculated, and that’s because code you added in setupCartObserver() is still watching the number of items in the cart. When that number changes, onNext is called, and the number updates automatically.

Does that help?

Hi my question how to work with singletone from different view controllers , if we have some view controllers that display data from singletone , how to subscribe to observer then for example 2 or more view controllers have elements subscribed to some data . In your tutorial on first view controller your subscribed to singletone , but if in second view controller will be element that can change it , or in third view controller , so that happen as ? how to make CartViewcontroller change data on 2 or more view controllers then his data changed ?
Sorry for my english.
PS. I know how to work in general with singletone , question how to work with RXSwift with observable.
PS. why let chocolates: Variable<[Chocolate]> = Variable([]) here your used Variable
let europeanChocolates = Observable.just(Chocolate.ofEurope)
both array , so why one time its Variable second time is Observable , that difference ?

Great tutorial! I’ve been waiting for a RxSwift tutorial on RW. Hopefully you guys will write a book on RxSwift. There are lots of tutorials out there for RxSwift but only a handful of them are easy to follow and comprehensive like this one.

1 Like

Hey Basconje, let me take your PS’s first since those are a little clearer.

In the ChocolatesOfTheWorldViewController, I used Observable.just because I know that Chocolate.ofEurope is not actually going to change its content - I’m mostly using RxSwift to set up the table view. This is why I added the note about just being a possible sign that using Rx is overkill.

In the cart, I set up chocolates as a Variable because I knew that value would change as the user added and removed chocolates to and from the cart.

I think, if I understand your main question correctly, you’re asking if it’s possible to add multiple view controllers as Observers of the cart. Yes! You can add as many Observers as you want to any Observable.

It’s a little bit hard to picture in the current example, but imagine the same application set up for use on an iPad, with the ChocolatesOfTheWorldViewController as the master view controller and the CartViewController as the detail view controller. If you add them both as Observers to the chocolates array on the cart, each one will receive updates whenever that value changes. They’re both listening for changes to the same value, even though the actions they will take when that value actually changes are different.

Does that help?

Yes its help.
Thanks a lot for explanation.

Thanks for the killer tutorial! However, you called a closure parameter “trailing” when it’s not actually trailing. Not a big deal, of course, but it could confuse some folks just learning Swift.

HI there,

Thank you very much for the tutorial. It is a bit hard to know what is really happening in background if you are not fit with generics (like me), but it is really cool.
I had crashes on the billing screen, because the validate function for the card number is changing the focus to the expiration date field (if the card number is correct). This does not work right on the following case:

  • Type a correct card number
  • Type a correct expiration date
  • Click on the card number field : Crash!. At this point we change the firstResponder to the expiration date field again, and RxSwift goes crazy.

I fixed it with:

unowned let welf = self;
let creditCardValid = creditCardNumberTextField
.throttle(throttleInterval, scheduler: MainScheduler.instance) //2
.map {
self.validate(cardText: welf.creditCardNumberTextField.text!)
} //3

Other topic. I find reactive programming really good to tie a model with a view. What I don´t see now are the advantages of using it for validation. Using text field delegates you have an entry point for all the changes happening on the text field. Maybe you can save some if statements, but nothing more. Or I am missing something? :S.


Thank you for that update @arodriguezju - I appreciate the time you took to figure that out! Good example of how sometimes the memory management with Rx can get a little hairy if you’re not careful.

I know one of the big things I run into trouble with in my day to day work is validating multiple UITextField forms when using traditional MVC / Delegate methods. When performing the “Should I allow the user to submit this data?” logic, I either wind up with a giant if/else statement in my delegate method deciding which validation to run based on what field has changed, or performing validation on every field every time anything in any field changes, which is a giant waste of the user’s battery.

To me, the advantage of using Rx for validation over traditional delegate methods is that you’re able to avoid having to run your validation on every field every time anything changes - only when that field changes do you need to update its boolean for validity, and then when all the booleans are updated, that automatically flips on the button which allows you to submit. No field needs to know anything else about the validity of any other field, and when that field changes, only its own validation is run without the need for a large, hideous if/else statement, or having to check a very large number of booleans manually.

Does that help?

1 Like

Thanks for this great tutorial. Here I am struggling with a crash. Once I filled all the details i.e. Credit card number, expiry date and cvv. After this purchase button got enabled. Then I again went to Credit card number field and app crashes. I tried to debug the cause of crash but failed. If anyone could help me would be great.

What happens when you crash? Have you added an exception breakpoint to your project?

If nothing happens when you crash - no console output, no opportunity to examine variables - then you probably need to add an exception breakpoint. Go to the breakpoint navigator (shortcut: ⌘7) and then click the ‘+’ at the bottom corner of the screen, then choose add exception breakpoint. Next time you crash you will hopefully be able to a little more information that will help you debug the problem.