Hey everybody this is Kayla with slopes.
And in this video we're going to be talking about what is our X what is reactive programming and what
is our X Swift.
We're also going to talk about why we should care as a developer why that's important to us to know
about and how it can help us to write better and just more fun applications I think are next what is
really fun to work with.
And there are some things you will learn in this course that will blow your mind.
Ok seriously when I first learned Ark's Swift I was amazed at just the abilities and some of the things
that I could do that normally would take a ton of time or just a lot of boilerplate code that is just
simply removed when using our swift.
So first of all I want to show you an article and I really recommend that you go back and you read through
all of this when we're done with this video because it's really really really good.
It's called the introduction to reactive programming.
You've been missing and this is language agnostic so it's not going to be specific to swift but that
might actually be helpful if you have experience in another language it might help you to read through
this and see how reactive programming can apply and be used in many other programming languages not
just swift.
So I want to scroll down and just explain to you what re-active programming is and there are some definitions
that are very very very very long very academic very detailed and very hard to understand but at its
core reactive programming is programming with asynchronous data streams like you may have heard me mentioned
in the previous video.
There there is something called an observable in Eric Swift.
OK.
That's not just our ex-wifes that's our acts in general but an observable is a data stream and observable
is something that can hold data.
And when you subscribe to one of these data streams the way that you go through the data is a synchronously
meaning you go from the beginning to the end and you don't skip or you don't jump and you don't go through
in a different order.
You go from the beginning until the end.
That's asynchronous.
So reactive programming is programming with asynchronous data streams.
Ok now it says here in a way this isn't anything new.
If you work with API eyes or really anything in swift or in programming a lot of what we do is asynchronous
if we tap a button it does one thing then it does another thing and another thing and another thing
and it goes from top to bottom you rely on that asynchronous order for the app or the program to do
what you want.
But re-active like it says here is that idea on steroids.
You can create data streams of anything not just clicks not just hover over events like in web development.
Anything can be a stream variables a tap a certain property data streams data structures like anything
can be a reactive stream of data that's the amazing thing about reactive programming now.
Like it says here for example imagine that your Twitter feed would be a stream of data in the same event
that click events are.
You can listen to that stream of data and react accordingly.
OK as it changes.
Now the coolest thing is that on top of just the ability to turn anything into a stream of data you
are given an amazing toolbox of functions and we're going to talk about those.
They're called Transformers.
At least that's what I call them.
But there are ways that we can transform data so we can basically take a stream of data and we can filter
it or combine it or zip it together with any other stream back to back to back to back.
So a single stream can basically go into another one and the output of that stream can go into another
stream and so on and so forth you can merge two streams together you can filter one stream so that you
can basically filter out ones that don't filter out elements that don't meet a certain condition.
You can map values from one stream into another one so you can modify them.
Maybe you want to take an input of certain values and multiply all of them by 10.
You can use Map to do that and then that can feed into another stream if you want to modify the data
again.
So it's really really cool how we can use asynchronous data streams and react to them accordingly and
use these amazing our X functions to modify them and make them exactly the kind of data that we want.
So that is reactive programming.
But why why should we be using our swift.
This is actually from the swift documentation which again as a programmer you should know how to read
documentation you should go look through this and read it yourself.
There's a lot of really good information here.
Now the why for ARC Swift are X allows us to build our apps in a declarative way can now take a look
at this.
You're not going to maybe understand what this is right from the get go but don't worry.
We'll dive into it together.
But basically what they are doing is they are binding and observable to a label.
OK so a label they're basically creating and as an observable they're using a function called combine
latest which is going to take these two variables.
OK.
Or maybe these two labels first name and last name label and then they are pulling out the R X property.
OK the re-active property that you get when you import Ark's swift and they're pulling out the text.
So basically they're turning this first name into an observable of type string from the text.
They're turning the last name label also into an observable and they're using combined latest to put
them together.
Now inside combined latest they can use the dollar sign zero to access the first element and dollar
sign one to access the second element and then they're combining them with a space in the middle to
create the full name.
Then what they do is they take a map.
And what they're doing is they're taking the value from combined latest and they're mapping it to a
string that says greetings and then it adds the full name with dollar signs zero at the end.
They're using bind and with bind.
They can actually take that data and bind it to a UI element.
So their greeting label they're binding the text from those to UI text fields and they are binding it
to a label.
It's so cool.
So this one singular line of code takes the place of tons of importing certain delegates of making Iby
actions and Iby outlets and basically setting all of maybe you have a function that can combine both
of the names.
But instead of that we can do one observable that observes when the the name changes.
It's going to add it to the label and then bind it to the label.
Super super cool.
A single line of code can do that.
It also works with table views and collection views.
You can take data and bind it to a table view the exact same way with a singular line of code that has
functions back to back to back to back member the inputs into one function becomes the output and that
output can go into another function and so on and so forth.
Very very cool.
Now of course there are some really cool ways that we can use this with API.
Like I mentioned.
So let's say we had a function that does something incredible for a certain person.
OK.
Now of course it's going to return an incredible thing.
But basically let's say that when you're working with this API maybe you want to make sure that if it
doesn't get a result back that it can retry retry is built into our X. swift if we don't get a result
back we can react to that and retry the same call again.
We can also said it so that you can avoid kind of API spamming.
You can set it so that in your app when you type in something maybe in a search bar you can set it so
that it pauses for maybe a half second or even a whole second before it actually processes the API call
so that you don't make like 50 API calls if you type 50 characters you could have it wait until you
have paused for point five seconds and it's just built in to our ex-wife.
That's the coolest part.
Now of course delegates there is a really expressive way to use delegates instead of using your standard
scroll view that scroll and maybe you're not really sure what scroll view that is.
Maybe you're not sure exactly what constraint you're dealing with.
Instead of doing that and it appears what they're doing here is they're using this on some type of table
view in order to modify the content offset or maybe the constraint based on the content offset instead
of doing that and you're not really sure what they're trying to do.
You can go into self dot results table view.
You can access the reactive property and you can go into the content offset then you can map that to
be whatever else.
So you can basically say the content offset X and we can bind that to the constant of the constraint.
So it's really easy in a single line of code.
We can specifically and very expressively do the same exact thing as whenever the scroll view scrolls.
You can set it the exact same way with a single line of code.
I hope that as you're seeing this your mind is being blown because it is amazing how much we can do
with so little in our acts.
And I'm going to continue to show that to show you that throughout this course and of course there's
plenty more you can do key value observers you can do notifications transients state stuff lots of cool
stuff in here and I really encourage you to go through and read all of this but I just want to show
you some of the just amazing like the biggest most amazing things that are X can help us with as a developer
I'm sure you're thinking like wow this is a huge huge thing and it is it's a big deal but it's also
kind of a big paradigm shift.
And one of the most challenging things for myself when learning our swift was to think reactively and
to think in a new way because it is kind of a paradigm shift.
We're gonna be doing things differently than you're used to so just prepare yourselves for that in advance.
But let's let's get started.
Guys we are ready to go.
So let's head over to the next video and we're going to dive into learning all about observables in
our X-Wing.
So I'll see in the next video.
Great job with this one.
Let's go over to the next video.