Everyone welcome back Johnny be here with Stopes dot com.
And in this lesson we are going to delete some comments.
All right.
So the last lesson we worked in our thoughts and comments cells to be able to add protocols and delegates
so that we can inform our main view controllers when we have tapped on one of our option menu images.
Right.
So now we have our delegate functions here comment options tapped and do we want to have happen if we
check out our mock up.
Then it looks like we have an alert you pop up with the ability to delete the comment later on we're
also going to add the functionality to edit the comment.
But first we're going to look at the delete.
All right so let's go ahead and let's do this.
So I'm not going to cut this because I don't like these functions being up here at the top.
And put this under our view did disappear right here.
OK.
All right.
We need to add an alert.
So we're going to let alert equal you are high alert controller and will initialize it here.
So we're going to set the title to it.
Hope needs to be a string added comment.
The message will be something like you can delete or edit.
And the preferred style is going to be that action sheet.
All right now we need to create our actions so we're going to start out with just the two.
We're going to have an add delete action and cancel action.
It's going to say let delete action call a UI alert action initialize it with a title and the title
is going to be delete comment and the style is going to be that default
and then for our handlers are going to give us a completion handlers we just press enter and say action
in code.
All right and this is where we're going to delete the comment.
OK.
But we'll get right back to that and close this bottom pain.
And I'm going to create the edit action right now even though we're not going to work on it yet.
But I will say let action you call a UI alert action and initialize it.
The title is going to be
added comment.
The style is not default.
And the handler are going to say action and hopes and then we'll just remove that code block there.
And then lastly the council is going to say let's cancel action equal alert action.
Initialize it is going to be console.
The style is.
Cancel
and the handler is nil and we go and then we just got to add these actions to our alert to say alert
not add action and we want to add the to the read action then we want to add the added action.
Now we want to add the Council action.
And finally we just presented some say present.
Here we go present view controller and the controller who is alert animated is true and completion is
all right.
And let's go ahead and test this just to make sure that this appears to run.
All right let's pick one that has comments and then right click on this.
There we go.
All right.
Very nice.
So these don't do anything yet.
So let's let's fix that.
All right so to delete a comment or a document in Firestar it's actually pretty easy.
All we got to do is get a reference to the document and call the delete function on it.
So what we're going to do is we're going to say fire store which is our variable that we declare up
here.
Fire started off our store when say fire store data collection and we're just going to make our way
down from collection to document to collection to document.
And so if start ref that document and that path is here self Dot
thought dot document ID and then we're going to go one layer deeper down to the comments collection
so common start riff and then finally to the comment itself and we get that from our delegate function
so comment DOD document ID and then we just call delete and we can call a completion here so delete
and then get our completion and rename this to be error.
And here in the code we can say if that error is equal to error.
So we'll just do a check if there was an error and if there was an error and to close this bottom pain
then we will debug printed and we'll say unable to delete comment and then we will print out the error
localized description else then we will just say alert that dismiss animated is true and completion
is no.
And we need solve for it here.
Self-taught fires aren't very good according to plan.
When we click on this then we will delete that comment.
I'm going to click on serious this thought that has four comments on it.
Click here and click here and say delete.
And boom it's gone.
And but if we go back I emphasized how many comments it had.
It had four comments and it still says that it has four comments.
Well why is that.
Well we are not updating the thought field for the number of comments.
All right.
So if you recall in the ad I thought VC when we added a or not the thought in the comments of VC when
we added a comment we had to go through this whole thing with the run transaction to be able to first
read the thought and get the thought.
Then we had to extract the number of comments from it and then we had to run the transaction update.
So that's kind of a pain but that's actually what we're going to have to do.
Again here we're going to have to run a transaction to delete the comment.
But first we are going to instead of incrementing the number of comments by one we are going to be decreasing
the number of comments by one.
All right.
So I still want to show you this way because you know it is just useful for you to know multiple ways
to do things.
So this is how you would do it if you didn't have to worry about updating any other field if you are
able to simply delete a document from fires or that you just get the reference to the document and call
the delete function and you have the option of having or not having a completion handler.
All right.
So that's how you can do it.
I'm going to just comment this out and then right down here we were going to do a transaction.
Now that's a lot of code to write.
And we already have it right here.
So I'm just going to copy this transaction right here all the way down to here.
Copy that and we're going to paste it right here.
Close this bottom pane hand and we're going to modify it according to our needs.
First off this needs to be self and all of this is the same we are getting the same document we are
getting the same information the number of comments.
The only thing that is different is that we are instead subtracting 1 from the number of comments.
And but this part right here.
This can all go away.
We are not creating a new document and we are not setting new data.
Instead what we are doing is you're calling a transaction not the leaked document.
And all we've got to do is get the document reference which we already have right here so if we just
copy from here a self of Firestorm collection we go down through the thoughts reference to the document
to the Commons and then to the document here that's going to copy that and paste that right here so
that we can get rid of any sort of comments that we need to.
And there we go.
And I'm just going to set this equal to a new very long I say let comment her equal and equal to this
guy right here and then I'm going to pass in the document reference here comment breath save that.
And here in the completion händler we're just going to say alert that dismiss animated is true and completion
is now.
All right so let's go ahead and run that.
And while that's building I'll just run through what we have done once more.
So we are running the transaction and remember in transactions you have to read and then right.
So we go ahead and we do a read we go and fetch the thought document and that this comment is a child
of.
And then we get the number of comments from that data.
We decrement the number of comments by one then we get a reference to the comment and we just call transaction
delete document.
All right so when we check this out.
So now it's at 4.
It should be 3.
But if I delete this comment then we should see it be three because it has a decrease the number by
one.
This one has a comment.
I'm going to delete it and it should now be had zero.
And this one has a comment.
I delete it then it's had zero.
All right.
Looks like it is working great for us.
All right so that is how you can delete documents in the fire store if you don't need to worry about
doing other reads and writes.
Then you can call this a simpler function which is just getting a reference to the document and then
calling delete if you need to handle it with along with other reads and writes then you're going to
need to run it in a transaction which is similar.
All you got to do is get the reference to the document and then call transaction delete document.
All right so yeah super useful to know.
Being able to delete data is you know something that you'll use very often in apps over again a lot
of value out of this.
And I will see you all in the next one where we start where we add the ability to edit the comment as
well.
All right.
So I'll see you then.