Hi, Alex here from rebeloper.com and welcome back to this tutorial series on
cloud firestore. Now in this little section I believe it will be two videos.
We will talk about advanced data management most importantly we are going
to take a look at transaction and batch rights. So without further ado let's jump
right into our first topic and that is batch rights, okay.
Now in the last episode
we have talked about incrementing a numeric value if you don't know what I'm
talking about go ahead and take a look at the previous episodes in this
humongous course on cloud firestore. You can see all the links down below to the
playlist and yeah so that is where we left off and today we are going to move
even further and we are going to take a look at not transactions but we are
going to take a look at a little bit more simple and that is batched rights.
Now what is the batched rights? Well basically we can sell update and delete in batch
and that means that let's say we have two separate collections let's say two
users or two cities in our case will be two cities and we want to let's
say change the names of those cities or maybe their favorite color or whatever.
We want to change both of them and if one change let's say it one right goes
into error so it cannot be written then we don't want to make the other change
either. So either both changes or more changes we can have as many batches as
we want well I believe there is a limit yeah contain of 500 operations, there we go.
So up to 500 batches, well operations
and if one of those does not succeed all of them will not be written if all
of them succeed then all of them will be written. So let's take a look at how this
goes in action in our Xcode. So let's open up our project and if you are new
here go ahead and take a look the links down below where you can find all of the
documentation and all of the reference to this project, okay. Well basically you
can find it on my github page, okay. Now what have I added since the last video?
Well first of all let's scroll a little bit up here because I have added
something to our add button tapped file privet function and that is I will
add not just the LA document with the name, state, country, and favorites. I will
add this New York document and of course we could add this in a badge but yeah
this is just adding one after the other and we are going to learn how to batched
right just down below in this new function and that is what I have added
two buttons one for badge rights and one for so here it is commit
patch and the other one is a run transaction. And we are going to create
transaction in that next video. So for now we have two references and that is
for one for the LA document and the other one is for the New York document.
And we have these two data and we are going to update the favorites color both
of them will be changed to green and I'm not going to scroll up but currently
they will be set as blue, okay. And if you don't know what this dot notation is
take a look at the update section of this long playlist, okay.
So let's just write some code, okay. Now first of all we need to create a batch and that
is pretty simple. Let's create a batch. Let badge equals firestore dot firestore
dot batch and that is basically it. We now have our batch. Next up we want to
commit set the data on this batch.
So batch set date and yeah we will use this one we are not going to bother by merging
this document and the data one will be here and for the document we are going
to use this reference one for LA, okay. And the same goes for the second set of
data and reference. There we go. So now that we have an all of these two set to
our batch we are simply just going to commit this batch. So badge commit and
let's see we have this without a completion handler and we have the one
with the completion handler of course I will choose the one with the completion
handler. So if there is any error we can handle that and as usual I would just
simply copy this error handling methods, there we go. So we can save on time. Let see.
There we go. Let's paste that in there. If there is any error we are just going to
print it out and if this commit succeeds and we will say successfully
committed batch. Let's just write that error, there we go.
So yeah that is it. Just to recap we have one reference and one data that we want
to update or set one rather yet let's just have
this to update it. Let's see if that up date data get that yeah it exists. So we
can set update or delete data and we are going to use this update because as
you recall from previous episodes if we said that it will delete everything
from a dead branch so we don't want to do that. We want to update the data, okay.
So we have two references with data, there we go.
That we want to add or update. We create a batch like so fire store dot fire
store dot batch and we do necessarily update set or delete data call on the
batch and as you can see we have two here. So then we finally commit our batch
and if there is an error we get back an error, okay. So and let's a build and run
and finally have this. What we are going to do here first
let's go to our database and as you can see here we have our LA and we are going
to add the New York document. So click on add here and as you can see here we have
our New York and LA, New York and the color is blue for New York and LA too, okay.
So let's go here and we will change the color both for LA and both for New York.
So let's see. Let's click on commit a batch as you can see the color is now
green for LA. Let's double check for New York.
It is green, yay and that is basically that is how simple it is to batch right
into cloud fire store. Now there is another way that you can write and it's
a more advanced write in cloud fire store and that is transaction.
When you want to not just write into the database but first read from the
database and then write according to what you have a read, okay. But that is a
topic for the next video if you did enjoy this one go ahead and give it a
thumbs up, subscribe to the channel and hit that notification bell so you get
notified of the transactions video and as usual I will see you in the next one.