Hey everyone welcome back to class.
Johnny be here with slopes dot com.
And in this lesson we are going to add our first comments and in the process learn about fire store
transactions.
All right.
So let's get started.
First thing I want to do is just run this and make sure that our segues are working.
We haven't tested that yet.
So we are passing from our main these when we click on a thought.
We were passing that thought here.
So just to make sure that is working let's go ahead and print out thought that comment or thought text.
Are you going to run that and when we click on one of the thoughts we should see printed out to our
log.
The thought that we couldn't get someone to click on this one.
And all right so it looks like that is working.
Don't give up on your dreams keep sleeping is printed out to our blog.
And before I forget let's go ahead and fix up our table.
I don't like these dividers and I know there's something else that we need to fix as well.
So jumping into our main story board let's hear table you selected and separator should be none.
And then on the Common to sell selection should be none.
There we go.
All right.
So now back into our comments visi.
So now that we have we know our Segway is working we have a thought variable here that we can reference.
So let's talk a little bit about how the architecture for this is going to work.
Now we know that entire store we have our data organized into collections and documents.
Collection holds a bunch of documents and each of those documents could have some collections.
So in this app we have thoughts a collection of thoughts and we have these documents that each of these
documents can have a subcollection called the commons and inside of that collection we will have more
documents that represent the comments that are added for that particular thought.
OK so that's kind of how it's going to work.
So first off let's go ahead and create a reference to the individual thought that is clicked on.
So here I'm going to say who are thought ref and this is going to be of type document.
Let's see here.
Oh I don't think we have fire so that's why.
So wasn't complaining because we don't have fires firebase imported yet.
There we go document reference and implicitly and unwrap that and then here in view to load and when
to say thought ref is equal to fire store.
And actually we can create a reference for fire store so that we don't have to write out fire store
fire store every time to save fire or is equal to a fire store that fires store.
And so here thought Refco going to say fires door at the Capitol One their fire store that collection.
All right.
This is the reference to the fact that we selected.
So looking at our database there would be the reference to one of these individual documents here.
OK.
So if I restore the collection the collection path is thoughts ref and then the document path is thought
that document ID.
So now we have a reference to the path to the thought that we can use throughout the rest of this class.
Next thing I want to do in preparation is create a variable for a username Katulis a variable user name.
And this is going to be a string and where we're going to get this is from our off.
So if you remember a couple of lessons ago in our create user Visi we actually set a display name that
we can access from our user.
So here comments VC I'm going to say there's going to be an option or someone to say if that name is
equal to off that off that current user.
OK so this gets the cached current user or gnoll if there is none.
OK so the current user.
And then we access the display name and then we're going to say a user name is equal to name.
Very good.
All right so now let's get to our ad comment tapped button.
OK so this is what it is called When we look to add a new comment.
And so first up let's go ahead and get our comment tecs.
So when we say God let comment text equal self-taught.
I don't mean this right here and say what did we call it we called it add comment text.
So add common text that text else return in case and now we have the text of the comment that we are
adding in this bottom pane here.
All right so now we are ready to add our comment.
So previously we just use the firestorm method and document which has a completion handler where we
add the data as a dictionary and then send it up.
Right.
And we could do that.
But the reason that we're going to use something else which I'll explain here in a second is that we
are also wanting to update the number of comments for this thought in our fire store database.
So we're adding a document but we also need to be adding incrementing a value in the thought document
as well.
And so we would be doing multiple reads and writes and that's actually best handled in something called
a transaction.
OK so when a transaction you can group multiple operations into a single transaction transaction consists
of any number of get operations followed by any number of right operations such as set update or delete.
All right so why do we need a transaction here.
Well just like I said we're going to be adding a comment as well as updating the field the value for
the thought.
But a couple of things to keep in mind.
For a transaction I read operations must come before right operations.
So first we're going to read from the thought we're going to get the number of comments that it currently
has.
And then we are going to update that field as well as add the new comment for it.
Rule number two transactions never apply partial rights.
So it's an all or nothing deal and transactions fail when user is off line.
So that's something to keep in mind and a transaction should not directly modify the application state
meaning that this transaction is not executed on the main thread.
With that all out of the way let's go ahead and code it up.
So to do so we just call fire store that run transaction and press return and we have a handler here
and we have a transaction and then error pointer.
And then here in the code and then we have another question handler here.
And this is we're just going to call this object and error in code.
OK so this transaction is what we're going to be working with here inside of the actual running of the
transaction.
And then here is an object that could be returned out of the transaction.
OK.
So go ahead and just say return nil.
We're not going to be returning anything out of the transaction.
All right so remember that reads need to occur before rights.
So first we're going to get the thought document so that we can know how many number of comments there
are so that we can add one to it when we add this comment.
OK.
So I'm going to create a thought document here type document snapshot and I need to declare it here
because we access it in a try block which I'll write out here.
OK.
So you can try her catch catch well-led error as in this error and then return nil.
And then now we're going to say Try thought document and my auto correct gets a little bit funky here
for some reason every time.
So just follow along closely so thought document is equal to trend's action dot net document.
And then we put in the path to the document which is a fire store and fire store
that going to drop this down to a new line.
That collection of thoughts underscore rough and I need a dot right here collection dot document.
And finally auto complete is back so there we go that document and the document is or the path is self-taught
thought that document Heidi.
All right so a little bit harrowing there for a minute.
I'm not sure why the autocomplete gets all freaky inside of this transaction but just to recap we say
lead thoughts document equal a document snapshot and then we try to get it from transaction dot document.
And then we pass in the path to it.
And then here we're just going to debug.
Print the error if there is one.
When I say Fetch ever and we're going to string interpolate the error dobbed localized description.
All right.
So now we have the thought document.
So what do we need out of it.
We need to know how many likes it currently has.
So we're going to say guard that old gnome likes people and we're going to access the thought document
fact document that data.
So we're going to get the data from it which is a dictionary and we're just going to grab it out of
that dictionary of the number of comments and we're going to cast this as an else returned and I just
realized that I said likes here this should be comments so old mom Common's is equal to thought document
data the number of comments cast it doesn't else return.
So basically all we're doing is what we have here but I need to return now.
There we go.
Basically all we're doing here is we're just drilling down from our thoughts to the document to the
number of comments and extracting that data and then we can just see transaction that update
data and the fields is numb underscore comments and the value is old comments plus one and forward document
the document reference is sulf that thot ref.
All right so now we have taken care of updating the number of comments for the thought that we have
clicked on when we add a comment.
All right next thing we need to do is actually add the comment.
OK.
So let's go and do that now.
In the past we have always used the audio generating document IDs.
However when you use set which you have to use in a transaction you have to explicitly set the ID and
so the workaround for that is to create an empty document first which uses the auto generating ID and
then using that.
All right.
So we're going to say let new comment ref equal self dot fire
store data collection and the collection path is thoughts ref.
Then we drill down one layer deeper to the document and that is at self-taught thought that document
ID and then we go one layer deeper to a new collection called the comments.
And let's go ahead and create a constant for this.
So I'm going to jump back and we're Constans file.
And here with our other references I'm going to say let comments ref equal comments.
Copy this gem back in the comments AVC and set the collection are to comments and then we just say document
that just kind of creates an empty document.
Now when we create our new comment document and we'll we'll pass this comment in the common ref.
OK.
So we're going to see transaction data set data and that's the one we want here for document.
The document here is the new comment ref.
And here the data that we're going to pass in is we're going to do the user name the timestamp and the
comment text.
So I'm going to say comment text and that is of value right here.
Comment text comma.
Then we're going to use the username or the time and this is a concept that we already have and that
is field value that server time stamp.
And then another common comma and the third value that we were adding to our comment is the user name.
So when I say user name and that is also user name and I can hear the self here.
Since we're inside of a closure and then this can turn this into a constant here.
So we're going to copy that as well and come into our Constans file here and here with the rest of the
field contents.
I'll say let comment underscore the extreme equal comment text and then I'll copy this constant here
jump back into comments Visi and replace that save that save our Constans file and back in the comments
visi.
I think that looks good.
So now let's handle if there is an error.
We'll just say if left error is equal to error we are just going to debug printed something to say debug
print and it's going to say transaction failed and print out the error.
Else if it was successful then we're just going to clear out the comment text field so that it's ready
for a new comment.
I going to say self that enter or add comment text that text is equal to an empty string.
All right so that looks good.
Let's go and just review real quick what we did.
All right so when someone clicks on the Add comment button what we do is we run a transaction.
And the reason we use transactions is when you have multiple read and writes all right.
And so with a transaction we have to read it first and so we did that here.
We do have a do catch block where we try to read from the database.
Our thought documents we say get document and then we pass in the path to the thought document.
If something goes wrong then we print it out and then from there from that document that we read we
get to the number of comments data since that's what we want to increment by one.
And as we get that information and then we update that data using the number of comments field and incrementing
it by one and then finally we actually get to the part where we are adding the new document and to do
that we first create a new comment a ref so that we can have an auto generated ID and we create the
new comment here by saying set data and we pass in the data that we want to create and set in our new
document and that is the common text timestamp username and then finally we handle the error if there
is one.
So let's go ahead and test this out.
I'm going to run it all right so I'm going to click on the serious one from Aristotle and I'm going
to write here.
Gonna go ahead and add a comment.
So keep in mind that right now we don't have the capability to.
If we wait if we toggle our keyboard here.
So if you come down a hardware keyboard and if you toggle the software keyboard that's going to cover
it up.
So just keep that keyboard on toggled for now.
We might get to fixing that later.
So when I added comments to say adding first comment when I pressed the button and it cleared.
So let's go ahead and check our.
And we don't get any error statements here so it's good to check our database and find that one.
Let's see here.
Here it is.
So now we can see this is the one that we commented on and it has a subcollection of comments and if
we click on it then we have documents and if we click on that document there it is adding first comment
right so that is pretty cool.
That's going to add a second one simulator and get a second hand added.
And here we can see that it was added right here.
All right.
Very cool.
All right so now we have learned all about transactions and how we can work with multiple read and writes
at once.
And yes now we are adding comments in the next lesson or we are going to work on pulling them down from
this subcollection and displaying them all right.
So I'll see you then.