Everyone are welcome back home Johnny be here with soapstar calm and in this lesson we're going to work
on leading thoughts are.
So we already have it where we can edit and delete comments but now we're going to take a look at the
thoughts.
I don't want to create one here.
I'm going to say.
Joining me here.
I'm going to post it up and put in a user name here.
And that reminds me we don't actually need that user name field anymore so let's get rid of it.
And the reason we don't need it is because we have access to the username as part of our often user.
OK so up here this user names are going to deleted and we are going to add a tough constraint of about
16 here.
You go.
That looks good.
And then we'll get a couple of errors here in the ADD thought the C C here should be.
Oh well we already we already said the user id We just didn't actually remove that you didn't even feel
like.
So another that's gone that's taken care of.
And we'll get back to what we were talking about which was here in the mean Visi and leading thoughts.
All right so let's take a look at our simulator again.
Here we have a thought and if we click here nothing happens right.
Because here in our mean Visi up here at the top where we have the thought options tapped as we make
sure this is working actually first from us it print more going say I thought that text I'm going to
print or run that again and click on that.
And sure enough it's saying it is printing out the thought text here Johnny B here.
All right but that's not what we really wanted to do.
Let's go ahead and cut this and move this function down a little bit further and see here and after
that you will disappear.
Good.
Repaste that and get rid of this print statement as well as this comment.
And this is where we want the alert to show if we check out our mockup.
We just have an alert view that says delete delete comment and we can click on delete common to delete
it.
All right so let's say a left.
Alert equal to UI alert controller and will initialize it give it a title of.
See here.
Delete the message will say do you want to delete your thought and the preferred style is action sheet.
Thinks we need to create the alert actions when to say let delete action equal and it's spelled that
correctly.
Will eat you.
And that is going to be an alert UI alert.
Action will initialize it.
It's a title and will say delete thought and the style is dot default and handler here are handlers
when going to press enter and then put in action.
And then here in the code this is where we are going to delete.
The thought will come back to that in just a second.
It will need a cancel action so let cancel action equal UI alert action.
The title
title is cancel style as docked cancel handler is no.
So now we need to add our actions to our alerts when the same alert that an action the first one to
be added is the delete action.
Then we need to stay alert to add action and will add the cancel action as well and then we just need
to present our other few controllers with the say present view controller and we want to present the
alert.
Animated is true and completion is.
All right now all that is left is to actually delete our thought so we just need to get a reference
from to say fire or dot fire store data collection and we're going to be in the thoughts ref that document.
And thanks to our delegate function we have a reference to the thought that it was tapped on.
So here you're going to say thought that document ID and then just say Dot delete.
We can have a completion handler here if we want and will get the error and we'll see if that ever is
equal to error.
Then we will debug.
Print and handles say could not delete thought and will print out the error description of a localized
description.
Else we are done and will just dismiss our alert true and no it's going to save this and run it.
All right so we have a thought here that we created it or a little bit earlier and I'm going to put
a couple of comments on here comment one and say yes and then comment to and add that as well.
And then I'm also going to open up our database here.
Let's find that common that I just added.
Let's see here.
OK right here.
So you see that we have a document with a thought and then we have a subcollection to this document.
All right.
And you may be realizing that I'm leading up to something here.
But when I delete this comment here delete thought.
Sure enough it deletes.
But check this out.
Are subcollection of comments that belonged to that thought is still here and in fact if we refresh
this
you'll see here that you'll see here that this thought is now kind of grayed out and italicised meaning
that this thought document it doesn't really exist anymore.
It's just kind of hanging out in limbo as well and our comments collection is still here.
So this is a characteristic of firestorm.
Their database architecture when you delete something that doesn't automatically delete any subcollection
or documents they're in.
All right.
So we need to kind of handle this on our own.
And the way to do that.
There isn't actually like a nice little story about collection and document dot delete subcollection
function.
We kind of have to write that whole function out on our own and it's a little bit longer.
And the fire store just gives it to us so we're just going to go straight to the source.
So if you just Google firestorm delete some collection and go right here deleting data from cloud fire
store.
And what we want is right here delete collections.
And this is to delete an entire collection or subcollection in classifiers to or retrieve all the documents
within the collection or subcollection and delete them if you have larger collections you may want to
delete the documents in smaller batches to avoid out of memory errors.
All right so we're just going to grab this entire function right here make sure you are in swift and
copy it and we're going to jump back into X code and right here under our thought options tab function.
We're just going to paste this delete function and let's kind of run through real quick.
I should say self delete.
All right so we got this function here that Firestar documentation gives us and the function is called
delete and we pass into the function a collection or reference a batch size number which has a default
of 100 and then we have a completion handler that returns an error.
And then here inside we have some common sense here it says limit the query to avoid out of memory errors
on large collections.
So if you have like really big collections then you want to do this in batches and that's what this
batch size variable is here as is when the leading a collection guaranteed to fit in memory Bashan can
be avoided entirely.
We're going to do that because our mix up collections are pretty small relatively.
And so then what we do is the we get the collection and we are going to call get documents which fetches
all of the documents in the collection.
This limit to is something that we haven't gone over quite yet.
But it can be used in pretty much any of the features that we've used instead.
Right next to like the order or the wares.
You can have dot limit too and that will limit the number of documents that you fetch to whatever you
pass into it.
All right and then all we do is we just do a check on the docs and make sure that we that we don't have
any errors then we're going to make sure that they are in fact more than one documents and the subcollection
that need to be deleted and then we're going to do what's called a batch and came batches something
else that we haven't quite covered yet.
But basically it's kind of like a transaction where we were able to do multiple reads and writes all
at once but with a batch simpler documentation and all it does is just writes though.
OK.
So we're going to do a whole bunch of rights in a row which is going to be doing delete things and we're
just going to throw those all in a batch and it's just going to go through and delete them all.
And so that's what we have right here doc said that documents for each were going to batch and delete
the document and then we call batch doc commit which actually starts the process of deleting them.
And then we just have a couple of checks here for an error and then we then we call it.
All right.
So pretty pretty straightforward.
So let's let's get to it.
So what we're going to do is here in the thought options tapped in the delete action instead of deleting
the comment.
First of all we're actually going to do was run and delete the subcollection comments first and then
we're going to delete the the thought kept.
So we are going to say self that the elite are right to call that function that we just imported and
the collection or reference is are just going to give it to the fire storm dumped fire stored collection.
And this is fots ref.
Then we're going to go to the document which is thought dot document ID.
And then lastly to the actual collection that we want to delete which is a subcollection of the thought
document and that is going to be comments ref OK and then our completion handler I'm going to name that
error and in code here we are just going to do a check on the errors and see if that error actually
we can just copy right from here from our old delete function.
And here we're going to say could not delete sub collection.
And then here in the completion handler instead of dismissing we're actually going to call this function
that we already wrote which deletes the actual thought.
OK so first we call self-taught delete to delete a collection.
In this case is the thought subcollection.
If it is successful then we go ahead and we delete the thought itself.
All right.
And let's go ahead and does this don't want to run.
Can to create a new common to real quick.
You comment and post it.
And we got ourselves a crash.
So what's happening here is that the this is still looking for that username textfield that I deleted.
And I think I was confused earlier when I was looking at the View Controller and I thought that we had
already taken care of the username part right here.
So instead of looking at this user name we want to look at is the display name value of our currently
off in user and that is here in the off dot dot.
Current User dot display name.
And that's an optional string.
So I'm just going to provide a no coalescing value of an empty string and then we're going to get rid
of this trick.
This guard statement here and we're also going to get rid of this outlet here and I'm going to build
that and make sure that that software can build succeeded.
So it's going run this again.
All right.
So I'm going to create a new post make sure that's working.
New post and post.
And sure enough here we go.
I just want to add a couple of comments here comment.
One added comment to add it and then I'm going to check out our database here and let's see here that
one that we just added And see here.
All right so here we are this is the new thought and we have a couple of comments.
So now let's see what happens when we delete that.
All right.
So we're going to pull this over here and I'm going to delete delete the thought.
And we should see these disappear first and then that actual thought document disappear as well.
All right so if we refresh this then that thought nor those that subcollection will be anywhere to be
seen.
But this one if you want to get rid of it all you got to do is select the document and say delete document
and that will delete all of the nested data.
So deleting it from here on the dashboard that will delete the document and it subcollection is just
just an F why.
All right so it looks pretty good.
Looks like that's working.
All right.
Great.
So now we are able to edit and delete comments and now we're also able to delete comments or thoughts
which actually we now know how to go and delete collections and subcollection as well.
But there is a little hitch here that I want to go over in the next video so I hope you're ready for
that and I'll see you in the next one.