1
00:00:04,830 --> 00:00:10,620
Everyone are welcome back home Johnny be here with soapstar calm and in this lesson we're going to work

2
00:00:10,620 --> 00:00:12,320
on leading thoughts are.

3
00:00:12,540 --> 00:00:18,510
So we already have it where we can edit and delete comments but now we're going to take a look at the

4
00:00:18,510 --> 00:00:19,260
thoughts.

5
00:00:19,350 --> 00:00:21,230
I don't want to create one here.

6
00:00:21,420 --> 00:00:22,670
I'm going to say.

7
00:00:23,520 --> 00:00:26,400
Joining me here.

8
00:00:26,400 --> 00:00:29,630
I'm going to post it up and put in a user name here.

9
00:00:29,850 --> 00:00:34,090
And that reminds me we don't actually need that user name field anymore so let's get rid of it.

10
00:00:34,320 --> 00:00:40,770
And the reason we don't need it is because we have access to the username as part of our often user.

11
00:00:40,890 --> 00:00:47,760
OK so up here this user names are going to deleted and we are going to add a tough constraint of about

12
00:00:47,760 --> 00:00:49,190
16 here.

13
00:00:49,560 --> 00:00:49,860
You go.

14
00:00:49,890 --> 00:00:50,990
That looks good.

15
00:00:51,060 --> 00:00:58,600
And then we'll get a couple of errors here in the ADD thought the C C here should be.

16
00:00:58,640 --> 00:01:04,260
Oh well we already we already said the user id We just didn't actually remove that you didn't even feel

17
00:01:04,360 --> 00:01:04,570
like.

18
00:01:04,580 --> 00:01:07,650
So another that's gone that's taken care of.

19
00:01:07,680 --> 00:01:14,330
And we'll get back to what we were talking about which was here in the mean Visi and leading thoughts.

20
00:01:14,340 --> 00:01:17,400
All right so let's take a look at our simulator again.

21
00:01:17,640 --> 00:01:23,320
Here we have a thought and if we click here nothing happens right.

22
00:01:23,700 --> 00:01:30,300
Because here in our mean Visi up here at the top where we have the thought options tapped as we make

23
00:01:30,300 --> 00:01:37,050
sure this is working actually first from us it print more going say I thought that text I'm going to

24
00:01:37,050 --> 00:01:43,540
print or run that again and click on that.

25
00:01:43,710 --> 00:01:49,470
And sure enough it's saying it is printing out the thought text here Johnny B here.

26
00:01:49,520 --> 00:01:51,720
All right but that's not what we really wanted to do.

27
00:01:51,720 --> 00:01:59,040
Let's go ahead and cut this and move this function down a little bit further and see here and after

28
00:01:59,500 --> 00:02:00,750
that you will disappear.

29
00:02:00,750 --> 00:02:01,680
Good.

30
00:02:01,970 --> 00:02:06,170
Repaste that and get rid of this print statement as well as this comment.

31
00:02:06,270 --> 00:02:09,820
And this is where we want the alert to show if we check out our mockup.

32
00:02:10,080 --> 00:02:15,750
We just have an alert view that says delete delete comment and we can click on delete common to delete

33
00:02:15,750 --> 00:02:16,620
it.

34
00:02:16,620 --> 00:02:18,480
All right so let's say a left.

35
00:02:18,480 --> 00:02:26,800
Alert equal to UI alert controller and will initialize it give it a title of.

36
00:02:26,840 --> 00:02:28,200
See here.

37
00:02:28,210 --> 00:02:39,490
Delete the message will say do you want to delete your thought and the preferred style is action sheet.

38
00:02:40,110 --> 00:02:47,840
Thinks we need to create the alert actions when to say let delete action equal and it's spelled that

39
00:02:47,850 --> 00:02:48,660
correctly.

40
00:02:48,890 --> 00:02:50,890
Will eat you.

41
00:02:51,240 --> 00:02:54,750
And that is going to be an alert UI alert.

42
00:02:54,790 --> 00:02:57,190
Action will initialize it.

43
00:02:57,440 --> 00:03:08,550
It's a title and will say delete thought and the style is dot default and handler here are handlers

44
00:03:08,550 --> 00:03:11,660
when going to press enter and then put in action.

45
00:03:12,150 --> 00:03:16,150
And then here in the code this is where we are going to delete.

46
00:03:16,230 --> 00:03:18,690
The thought will come back to that in just a second.

47
00:03:18,820 --> 00:03:25,770
It will need a cancel action so let cancel action equal UI alert action.

48
00:03:25,770 --> 00:03:26,760
The title

49
00:03:29,190 --> 00:03:37,700
title is cancel style as docked cancel handler is no.

50
00:03:38,250 --> 00:03:44,820
So now we need to add our actions to our alerts when the same alert that an action the first one to

51
00:03:45,360 --> 00:03:47,880
be added is the delete action.

52
00:03:47,880 --> 00:03:54,750
Then we need to stay alert to add action and will add the cancel action as well and then we just need

53
00:03:54,750 --> 00:04:00,180
to present our other few controllers with the say present view controller and we want to present the

54
00:04:00,180 --> 00:04:01,080
alert.

55
00:04:01,080 --> 00:04:04,400
Animated is true and completion is.

56
00:04:05,060 --> 00:04:10,860
All right now all that is left is to actually delete our thought so we just need to get a reference

57
00:04:11,110 --> 00:04:21,720
from to say fire or dot fire store data collection and we're going to be in the thoughts ref that document.

58
00:04:22,320 --> 00:04:27,770
And thanks to our delegate function we have a reference to the thought that it was tapped on.

59
00:04:27,990 --> 00:04:34,530
So here you're going to say thought that document ID and then just say Dot delete.

60
00:04:34,670 --> 00:04:40,890
We can have a completion handler here if we want and will get the error and we'll see if that ever is

61
00:04:40,980 --> 00:04:45,540
equal to error.

62
00:04:46,250 --> 00:04:48,460
Then we will debug.

63
00:04:48,470 --> 00:04:59,810
Print and handles say could not delete thought and will print out the error description of a localized

64
00:04:59,810 --> 00:05:01,430
description.

65
00:05:01,430 --> 00:05:11,560
Else we are done and will just dismiss our alert true and no it's going to save this and run it.

66
00:05:11,840 --> 00:05:17,210
All right so we have a thought here that we created it or a little bit earlier and I'm going to put

67
00:05:17,210 --> 00:05:28,530
a couple of comments on here comment one and say yes and then comment to and add that as well.

68
00:05:29,090 --> 00:05:33,080
And then I'm also going to open up our database here.

69
00:05:33,470 --> 00:05:35,910
Let's find that common that I just added.

70
00:05:36,180 --> 00:05:37,830
Let's see here.

71
00:05:38,240 --> 00:05:39,100
OK right here.

72
00:05:39,320 --> 00:05:50,440
So you see that we have a document with a thought and then we have a subcollection to this document.

73
00:05:50,730 --> 00:05:51,010
All right.

74
00:05:51,020 --> 00:05:54,130
And you may be realizing that I'm leading up to something here.

75
00:05:54,380 --> 00:05:58,670
But when I delete this comment here delete thought.

76
00:05:59,300 --> 00:06:00,860
Sure enough it deletes.

77
00:06:01,250 --> 00:06:03,510
But check this out.

78
00:06:03,560 --> 00:06:11,660
Are subcollection of comments that belonged to that thought is still here and in fact if we refresh

79
00:06:11,660 --> 00:06:12,170
this

80
00:06:16,360 --> 00:06:28,120
you'll see here that you'll see here that this thought is now kind of grayed out and italicised meaning

81
00:06:28,120 --> 00:06:32,200
that this thought document it doesn't really exist anymore.

82
00:06:32,320 --> 00:06:38,070
It's just kind of hanging out in limbo as well and our comments collection is still here.

83
00:06:38,080 --> 00:06:40,140
So this is a characteristic of firestorm.

84
00:06:40,170 --> 00:06:47,140
Their database architecture when you delete something that doesn't automatically delete any subcollection

85
00:06:47,200 --> 00:06:49,710
or documents they're in.

86
00:06:49,720 --> 00:06:50,100
All right.

87
00:06:50,170 --> 00:06:54,230
So we need to kind of handle this on our own.

88
00:06:54,700 --> 00:06:56,530
And the way to do that.

89
00:06:56,770 --> 00:07:03,970
There isn't actually like a nice little story about collection and document dot delete subcollection

90
00:07:04,030 --> 00:07:05,790
function.

91
00:07:05,860 --> 00:07:11,130
We kind of have to write that whole function out on our own and it's a little bit longer.

92
00:07:11,260 --> 00:07:16,410
And the fire store just gives it to us so we're just going to go straight to the source.

93
00:07:16,400 --> 00:07:25,810
So if you just Google firestorm delete some collection and go right here deleting data from cloud fire

94
00:07:25,810 --> 00:07:28,060
store.

95
00:07:28,060 --> 00:07:32,760
And what we want is right here delete collections.

96
00:07:32,910 --> 00:07:38,500
And this is to delete an entire collection or subcollection in classifiers to or retrieve all the documents

97
00:07:38,500 --> 00:07:43,450
within the collection or subcollection and delete them if you have larger collections you may want to

98
00:07:43,450 --> 00:07:48,100
delete the documents in smaller batches to avoid out of memory errors.

99
00:07:48,100 --> 00:07:54,310
All right so we're just going to grab this entire function right here make sure you are in swift and

100
00:07:54,310 --> 00:08:02,560
copy it and we're going to jump back into X code and right here under our thought options tab function.

101
00:08:02,650 --> 00:08:08,980
We're just going to paste this delete function and let's kind of run through real quick.

102
00:08:09,020 --> 00:08:11,480
I should say self delete.

103
00:08:11,500 --> 00:08:16,240
All right so we got this function here that Firestar documentation gives us and the function is called

104
00:08:16,250 --> 00:08:23,050
delete and we pass into the function a collection or reference a batch size number which has a default

105
00:08:23,050 --> 00:08:28,380
of 100 and then we have a completion handler that returns an error.

106
00:08:28,510 --> 00:08:35,470
And then here inside we have some common sense here it says limit the query to avoid out of memory errors

107
00:08:35,470 --> 00:08:36,480
on large collections.

108
00:08:36,550 --> 00:08:40,690
So if you have like really big collections then you want to do this in batches and that's what this

109
00:08:40,690 --> 00:08:46,990
batch size variable is here as is when the leading a collection guaranteed to fit in memory Bashan can

110
00:08:46,990 --> 00:08:48,750
be avoided entirely.

111
00:08:48,760 --> 00:08:52,530
We're going to do that because our mix up collections are pretty small relatively.

112
00:08:52,810 --> 00:09:00,970
And so then what we do is the we get the collection and we are going to call get documents which fetches

113
00:09:00,970 --> 00:09:03,360
all of the documents in the collection.

114
00:09:03,520 --> 00:09:08,200
This limit to is something that we haven't gone over quite yet.

115
00:09:08,560 --> 00:09:11,860
But it can be used in pretty much any of the features that we've used instead.

116
00:09:12,280 --> 00:09:16,350
Right next to like the order or the wares.

117
00:09:16,540 --> 00:09:22,420
You can have dot limit too and that will limit the number of documents that you fetch to whatever you

118
00:09:22,420 --> 00:09:23,820
pass into it.

119
00:09:23,820 --> 00:09:28,630
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

120
00:09:28,630 --> 00:09:34,480
any errors then we're going to make sure that they are in fact more than one documents and the subcollection

121
00:09:34,480 --> 00:09:39,550
that need to be deleted and then we're going to do what's called a batch and came batches something

122
00:09:39,550 --> 00:09:41,470
else that we haven't quite covered yet.

123
00:09:41,800 --> 00:09:48,070
But basically it's kind of like a transaction where we were able to do multiple reads and writes all

124
00:09:48,070 --> 00:09:54,460
at once but with a batch simpler documentation and all it does is just writes though.

125
00:09:54,490 --> 00:09:54,970
OK.

126
00:09:55,030 --> 00:10:01,570
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

127
00:10:01,570 --> 00:10:05,080
just going to throw those all in a batch and it's just going to go through and delete them all.

128
00:10:05,180 --> 00:10:10,090
And so that's what we have right here doc said that documents for each were going to batch and delete

129
00:10:10,210 --> 00:10:15,970
the document and then we call batch doc commit which actually starts the process of deleting them.

130
00:10:15,970 --> 00:10:21,830
And then we just have a couple of checks here for an error and then we then we call it.

131
00:10:21,970 --> 00:10:22,710
All right.

132
00:10:23,110 --> 00:10:25,630
So pretty pretty straightforward.

133
00:10:25,870 --> 00:10:27,540
So let's let's get to it.

134
00:10:27,760 --> 00:10:35,020
So what we're going to do is here in the thought options tapped in the delete action instead of deleting

135
00:10:35,080 --> 00:10:35,500
the comment.

136
00:10:35,500 --> 00:10:40,000
First of all we're actually going to do was run and delete the subcollection comments first and then

137
00:10:40,000 --> 00:10:43,130
we're going to delete the the thought kept.

138
00:10:43,240 --> 00:10:50,290
So we are going to say self that the elite are right to call that function that we just imported and

139
00:10:50,320 --> 00:10:59,040
the collection or reference is are just going to give it to the fire storm dumped fire stored collection.

140
00:10:59,340 --> 00:11:01,180
And this is fots ref.

141
00:11:01,190 --> 00:11:09,180
Then we're going to go to the document which is thought dot document ID.

142
00:11:09,460 --> 00:11:14,230
And then lastly to the actual collection that we want to delete which is a subcollection of the thought

143
00:11:14,410 --> 00:11:22,540
document and that is going to be comments ref OK and then our completion handler I'm going to name that

144
00:11:22,570 --> 00:11:30,330
error and in code here we are just going to do a check on the errors and see if that error actually

145
00:11:30,340 --> 00:11:37,410
we can just copy right from here from our old delete function.

146
00:11:37,600 --> 00:11:42,340
And here we're going to say could not delete sub collection.

147
00:11:43,150 --> 00:11:50,920
And then here in the completion handler instead of dismissing we're actually going to call this function

148
00:11:50,920 --> 00:11:53,550
that we already wrote which deletes the actual thought.

149
00:11:53,700 --> 00:11:58,930
OK so first we call self-taught delete to delete a collection.

150
00:11:58,930 --> 00:12:01,380
In this case is the thought subcollection.

151
00:12:01,570 --> 00:12:07,520
If it is successful then we go ahead and we delete the thought itself.

152
00:12:07,820 --> 00:12:08,180
All right.

153
00:12:08,200 --> 00:12:11,110
And let's go ahead and does this don't want to run.

154
00:12:13,870 --> 00:12:15,970
Can to create a new common to real quick.

155
00:12:16,040 --> 00:12:18,880
You comment and post it.

156
00:12:22,190 --> 00:12:24,400
And we got ourselves a crash.

157
00:12:24,420 --> 00:12:29,820
So what's happening here is that the this is still looking for that username textfield that I deleted.

158
00:12:30,060 --> 00:12:34,590
And I think I was confused earlier when I was looking at the View Controller and I thought that we had

159
00:12:34,590 --> 00:12:37,060
already taken care of the username part right here.

160
00:12:37,110 --> 00:12:43,100
So instead of looking at this user name we want to look at is the display name value of our currently

161
00:12:43,100 --> 00:12:47,610
off in user and that is here in the off dot dot.

162
00:12:47,760 --> 00:12:50,030
Current User dot display name.

163
00:12:50,040 --> 00:12:51,610
And that's an optional string.

164
00:12:51,960 --> 00:12:57,870
So I'm just going to provide a no coalescing value of an empty string and then we're going to get rid

165
00:12:57,870 --> 00:12:59,490
of this trick.

166
00:12:59,570 --> 00:13:07,170
This guard statement here and we're also going to get rid of this outlet here and I'm going to build

167
00:13:07,170 --> 00:13:11,420
that and make sure that that software can build succeeded.

168
00:13:11,460 --> 00:13:13,650
So it's going run this again.

169
00:13:13,710 --> 00:13:13,930
All right.

170
00:13:13,930 --> 00:13:16,140
So I'm going to create a new post make sure that's working.

171
00:13:16,140 --> 00:13:18,480
New post and post.

172
00:13:19,020 --> 00:13:20,490
And sure enough here we go.

173
00:13:20,700 --> 00:13:24,520
I just want to add a couple of comments here comment.

174
00:13:24,590 --> 00:13:40,290
One added comment to add it and then I'm going to check out our database here and let's see here that

175
00:13:40,290 --> 00:13:44,440
one that we just added And see here.

176
00:13:44,880 --> 00:13:49,740
All right so here we are this is the new thought and we have a couple of comments.

177
00:13:49,770 --> 00:13:54,760
So now let's see what happens when we delete that.

178
00:13:55,740 --> 00:13:56,020
All right.

179
00:13:56,020 --> 00:14:01,050
So we're going to pull this over here and I'm going to delete delete the thought.

180
00:14:01,150 --> 00:14:07,560
And we should see these disappear first and then that actual thought document disappear as well.

181
00:14:07,560 --> 00:14:14,910
All right so if we refresh this then that thought nor those that subcollection will be anywhere to be

182
00:14:14,910 --> 00:14:15,650
seen.

183
00:14:16,230 --> 00:14:22,980
But this one if you want to get rid of it all you got to do is select the document and say delete document

184
00:14:23,010 --> 00:14:26,790
and that will delete all of the nested data.

185
00:14:26,880 --> 00:14:31,650
So deleting it from here on the dashboard that will delete the document and it subcollection is just

186
00:14:31,880 --> 00:14:33,230
just an F why.

187
00:14:33,750 --> 00:14:35,730
All right so it looks pretty good.

188
00:14:35,730 --> 00:14:37,680
Looks like that's working.

189
00:14:37,740 --> 00:14:37,950
All right.

190
00:14:37,950 --> 00:14:38,220
Great.

191
00:14:38,220 --> 00:14:45,150
So now we are able to edit and delete comments and now we're also able to delete comments or thoughts

192
00:14:45,450 --> 00:14:51,670
which actually we now know how to go and delete collections and subcollection as well.

193
00:14:51,930 --> 00:14:59,250
But there is a little hitch here that I want to go over in the next video so I hope you're ready for

194
00:14:59,250 --> 00:15:00,750
that and I'll see you in the next one.
