Join Jon Bott for an in-depth discussion in this video Variables, part of RxSwift: Design Patterns for iOS Developers.
- [Narrator] Now that the application is building, let's start exploring the different type of observables that we can use. So let's come into here and create a new folder, and we'll just call this Simple Examples. And in there, we'll create a file, and we'll call it SimpleRx. And let's go ahead and clean up this, and import RxSwift immediately, 'cause we're going to be using that. Make a class, and we'll call it SimpleRx, and in here, I'm going to make a bag, and we talked about dispose bags before, but this is just a container that will have all of our subscriptions, so that when this class de-inits, this bag will clear up all the subscriptions so that we don't have any memory leaks.
I'll just make a mark saying that we're working with variables. Extension SimpleRx. And in here, I'm going to make an instance, I'll call it static var shared. Normally you would inject things, but we're keeping this simple, so we're using single tins, SimpleRx, and we'll make the function called variables. And we will say print, see, one, two, three, four, five, six, variable, and that...
go ahead and remove some spaces. Let someInfo, this is how you create a variable. You just say that it's a variable, and you give it a default value. Variables are generic, so depending on what value we initialize it with, it is of that type. So this will be a variable of a string. We'll just say some value. All we're going to show is how we can get this value out of this variable container, how we can set it, and how we can subscribe to any changes with it. So in here, we'll say, print someInfo a value is equal to someInfo dot value.
And just calling the dot value gets the value out of the variable container. We can also assign it to other things. And now plainString is just a string, it's not a variable or a container. It just got the value out of it. PlainString is equal to... And in here we're expecting some value. And then to assign it, we're just going to assign something to that value property.
We'll say something new, and again, we'll say someInfo value is equal to someInfo dot value. So it reads almost exactly like a normal variable. Here we're going to expect something new, clean this up a little bit. And the last thing that we can do here is create a subscription to that variable.
I'll just say someInfo dot as observable, dot subscribe, and let's go ahead and collapse the side panel. We can do the on next property, I'll just say new value. And in here, I 'll just print "value has changed," and then, remember with variables, we can't really get an on error or on completed event.
We can get an on disposed event for this subscription, so I'll just say that it's an optional cleanup block. So if you have any resources that are being used in this subscription, you can clean them up at this time. And we're getting a warning here, and this warning is telling us that we've forgotten to do one of these steps. And that is adding this subscription to the dispose bag. So I'll just say dot disposed by bag, and this makes it so that when this class disappears, everything gets cleaned up correctly.
I'll just put a note here, "Variables will never receive on error and on complete events." Let's go ahead and consume this, we'll open up at the app delegate, we'll just come into here and say, "simpleRx dot shared dot variables," should be able to run this. Bring up our input panel, and we can see, just like as our code said, we were printing out some value when we assign it to the plain string, we got some value as well.
We update it to the new value, and then when we subscribed, we are able to see that it gets that new value. If we came down to here, and we reassigned this someInfo dot value, to something again, we should see that line 26 is triggered once again for this new value change. And you can see that that has happened at the bottom of the output.
Variables are really easy to work with because you can treat them like normal variables most of the time. And then you can subscribe to them, when you really care about the events changing.
- RxSwift syntax
- Creating simple observables
- Architecting the model layer
- Using Core Data
- Grouping tasks
- Changing UI
- Threading in the model and UI