1
00:00:04,590 --> 00:00:08,430
Everyone welcome back Johnny be here with Stopes dot com.

2
00:00:08,580 --> 00:00:11,340
And in this lesson we are going to delete some comments.

3
00:00:11,350 --> 00:00:11,660
All right.

4
00:00:11,740 --> 00:00:19,020
So the last lesson we worked in our thoughts and comments cells to be able to add protocols and delegates

5
00:00:19,110 --> 00:00:27,420
so that we can inform our main view controllers when we have tapped on one of our option menu images.

6
00:00:27,450 --> 00:00:28,010
Right.

7
00:00:28,110 --> 00:00:34,860
So now we have our delegate functions here comment options tapped and do we want to have happen if we

8
00:00:34,860 --> 00:00:36,570
check out our mock up.

9
00:00:36,750 --> 00:00:43,680
Then it looks like we have an alert you pop up with the ability to delete the comment later on we're

10
00:00:43,680 --> 00:00:46,510
also going to add the functionality to edit the comment.

11
00:00:46,560 --> 00:00:48,770
But first we're going to look at the delete.

12
00:00:48,780 --> 00:00:51,790
All right so let's go ahead and let's do this.

13
00:00:51,790 --> 00:00:58,590
So I'm not going to cut this because I don't like these functions being up here at the top.

14
00:00:58,810 --> 00:01:03,440
And put this under our view did disappear right here.

15
00:01:03,610 --> 00:01:04,430
OK.

16
00:01:04,940 --> 00:01:06,040
All right.

17
00:01:06,270 --> 00:01:07,510
We need to add an alert.

18
00:01:07,590 --> 00:01:16,300
So we're going to let alert equal you are high alert controller and will initialize it here.

19
00:01:16,500 --> 00:01:18,990
So we're going to set the title to it.

20
00:01:19,070 --> 00:01:23,650
Hope needs to be a string added comment.

21
00:01:23,880 --> 00:01:30,760
The message will be something like you can delete or edit.

22
00:01:31,180 --> 00:01:36,870
And the preferred style is going to be that action sheet.

23
00:01:36,880 --> 00:01:40,530
All right now we need to create our actions so we're going to start out with just the two.

24
00:01:40,530 --> 00:01:43,300
We're going to have an add delete action and cancel action.

25
00:01:43,380 --> 00:01:54,030
It's going to say let delete action call a UI alert action initialize it with a title and the title

26
00:01:54,030 --> 00:02:01,020
is going to be delete comment and the style is going to be that default

27
00:02:03,770 --> 00:02:10,790
and then for our handlers are going to give us a completion handlers we just press enter and say action

28
00:02:11,480 --> 00:02:12,560
in code.

29
00:02:12,590 --> 00:02:16,260
All right and this is where we're going to delete the comment.

30
00:02:16,460 --> 00:02:17,030
OK.

31
00:02:17,300 --> 00:02:21,100
But we'll get right back to that and close this bottom pain.

32
00:02:21,370 --> 00:02:25,890
And I'm going to create the edit action right now even though we're not going to work on it yet.

33
00:02:26,120 --> 00:02:34,190
But I will say let action you call a UI alert action and initialize it.

34
00:02:34,220 --> 00:02:35,480
The title is going to be

35
00:02:38,680 --> 00:02:40,260
added comment.

36
00:02:40,620 --> 00:02:42,420
The style is not default.

37
00:02:42,460 --> 00:02:50,640
And the handler are going to say action and hopes and then we'll just remove that code block there.

38
00:02:51,010 --> 00:02:58,880
And then lastly the council is going to say let's cancel action equal alert action.

39
00:02:58,910 --> 00:03:02,590
Initialize it is going to be console.

40
00:03:02,670 --> 00:03:04,710
The style is.

41
00:03:05,080 --> 00:03:05,860
Cancel

42
00:03:09,080 --> 00:03:18,440
and the handler is nil and we go and then we just got to add these actions to our alert to say alert

43
00:03:18,590 --> 00:03:29,740
not add action and we want to add the to the read action then we want to add the added action.

44
00:03:29,770 --> 00:03:36,740
Now we want to add the Council action.

45
00:03:36,740 --> 00:03:40,820
And finally we just presented some say present.

46
00:03:41,130 --> 00:03:48,560
Here we go present view controller and the controller who is alert animated is true and completion is

47
00:03:52,690 --> 00:03:53,060
all right.

48
00:03:53,170 --> 00:03:58,490
And let's go ahead and test this just to make sure that this appears to run.

49
00:03:58,540 --> 00:04:03,410
All right let's pick one that has comments and then right click on this.

50
00:04:03,460 --> 00:04:04,270
There we go.

51
00:04:04,270 --> 00:04:04,790
All right.

52
00:04:04,870 --> 00:04:05,770
Very nice.

53
00:04:05,770 --> 00:04:08,570
So these don't do anything yet.

54
00:04:08,590 --> 00:04:10,770
So let's let's fix that.

55
00:04:10,810 --> 00:04:16,870
All right so to delete a comment or a document in Firestar it's actually pretty easy.

56
00:04:16,880 --> 00:04:22,180
All we got to do is get a reference to the document and call the delete function on it.

57
00:04:22,180 --> 00:04:28,030
So what we're going to do is we're going to say fire store which is our variable that we declare up

58
00:04:28,030 --> 00:04:28,350
here.

59
00:04:28,360 --> 00:04:35,890
Fire started off our store when say fire store data collection and we're just going to make our way

60
00:04:35,890 --> 00:04:39,600
down from collection to document to collection to document.

61
00:04:39,730 --> 00:04:46,760
And so if start ref that document and that path is here self Dot

62
00:04:49,570 --> 00:05:01,280
thought dot document ID and then we're going to go one layer deeper down to the comments collection

63
00:05:01,300 --> 00:05:11,460
so common start riff and then finally to the comment itself and we get that from our delegate function

64
00:05:11,470 --> 00:05:20,440
so comment DOD document ID and then we just call delete and we can call a completion here so delete

65
00:05:20,620 --> 00:05:25,240
and then get our completion and rename this to be error.

66
00:05:25,750 --> 00:05:31,520
And here in the code we can say if that error is equal to error.

67
00:05:31,600 --> 00:05:36,820
So we'll just do a check if there was an error and if there was an error and to close this bottom pain

68
00:05:37,270 --> 00:05:48,200
then we will debug printed and we'll say unable to delete comment and then we will print out the error

69
00:05:52,080 --> 00:06:02,010
localized description else then we will just say alert that dismiss animated is true and completion

70
00:06:02,010 --> 00:06:03,340
is no.

71
00:06:03,960 --> 00:06:05,730
And we need solve for it here.

72
00:06:05,730 --> 00:06:10,320
Self-taught fires aren't very good according to plan.

73
00:06:10,320 --> 00:06:13,830
When we click on this then we will delete that comment.

74
00:06:14,220 --> 00:06:19,050
I'm going to click on serious this thought that has four comments on it.

75
00:06:19,050 --> 00:06:22,990
Click here and click here and say delete.

76
00:06:23,490 --> 00:06:25,450
And boom it's gone.

77
00:06:25,610 --> 00:06:28,980
And but if we go back I emphasized how many comments it had.

78
00:06:28,980 --> 00:06:33,000
It had four comments and it still says that it has four comments.

79
00:06:33,010 --> 00:06:34,750
Well why is that.

80
00:06:34,890 --> 00:06:42,050
Well we are not updating the thought field for the number of comments.

81
00:06:42,060 --> 00:06:42,300
All right.

82
00:06:42,300 --> 00:06:50,760
So if you recall in the ad I thought VC when we added a or not the thought in the comments of VC when

83
00:06:50,760 --> 00:06:57,810
we added a comment we had to go through this whole thing with the run transaction to be able to first

84
00:06:57,810 --> 00:07:02,160
read the thought and get the thought.

85
00:07:02,230 --> 00:07:09,360
Then we had to extract the number of comments from it and then we had to run the transaction update.

86
00:07:09,360 --> 00:07:12,660
So that's kind of a pain but that's actually what we're going to have to do.

87
00:07:12,660 --> 00:07:17,480
Again here we're going to have to run a transaction to delete the comment.

88
00:07:17,520 --> 00:07:25,200
But first we are going to instead of incrementing the number of comments by one we are going to be decreasing

89
00:07:25,560 --> 00:07:28,770
the number of comments by one.

90
00:07:28,770 --> 00:07:29,200
All right.

91
00:07:29,220 --> 00:07:38,820
So I still want to show you this way because you know it is just useful for you to know multiple ways

92
00:07:38,820 --> 00:07:41,030
to do things.

93
00:07:41,160 --> 00:07:45,540
So this is how you would do it if you didn't have to worry about updating any other field if you are

94
00:07:45,540 --> 00:07:54,150
able to simply delete a document from fires or that you just get the reference to the document and call

95
00:07:54,150 --> 00:08:00,200
the delete function and you have the option of having or not having a completion handler.

96
00:08:00,200 --> 00:08:00,840
All right.

97
00:08:01,050 --> 00:08:02,660
So that's how you can do it.

98
00:08:02,730 --> 00:08:09,950
I'm going to just comment this out and then right down here we were going to do a transaction.

99
00:08:09,990 --> 00:08:12,400
Now that's a lot of code to write.

100
00:08:12,420 --> 00:08:14,520
And we already have it right here.

101
00:08:14,520 --> 00:08:20,520
So I'm just going to copy this transaction right here all the way down to here.

102
00:08:20,520 --> 00:08:26,150
Copy that and we're going to paste it right here.

103
00:08:26,300 --> 00:08:33,760
Close this bottom pane hand and we're going to modify it according to our needs.

104
00:08:33,780 --> 00:08:42,800
First off this needs to be self and all of this is the same we are getting the same document we are

105
00:08:42,800 --> 00:08:46,010
getting the same information the number of comments.

106
00:08:46,010 --> 00:08:52,770
The only thing that is different is that we are instead subtracting 1 from the number of comments.

107
00:08:53,360 --> 00:08:54,930
And but this part right here.

108
00:08:54,950 --> 00:08:56,840
This can all go away.

109
00:08:57,260 --> 00:09:00,720
We are not creating a new document and we are not setting new data.

110
00:09:02,280 --> 00:09:08,640
Instead what we are doing is you're calling a transaction not the leaked document.

111
00:09:08,880 --> 00:09:14,640
And all we've got to do is get the document reference which we already have right here so if we just

112
00:09:14,640 --> 00:09:20,160
copy from here a self of Firestorm collection we go down through the thoughts reference to the document

113
00:09:20,550 --> 00:09:28,050
to the Commons and then to the document here that's going to copy that and paste that right here so

114
00:09:28,050 --> 00:09:33,740
that we can get rid of any sort of comments that we need to.

115
00:09:33,850 --> 00:09:36,280
And there we go.

116
00:09:36,750 --> 00:09:46,700
And I'm just going to set this equal to a new very long I say let comment her equal and equal to this

117
00:09:46,700 --> 00:09:54,730
guy right here and then I'm going to pass in the document reference here comment breath save that.

118
00:09:55,170 --> 00:10:04,380
And here in the completion händler we're just going to say alert that dismiss animated is true and completion

119
00:10:04,380 --> 00:10:05,970
is now.

120
00:10:06,060 --> 00:10:07,760
All right so let's go ahead and run that.

121
00:10:07,890 --> 00:10:12,670
And while that's building I'll just run through what we have done once more.

122
00:10:12,930 --> 00:10:19,620
So we are running the transaction and remember in transactions you have to read and then right.

123
00:10:19,650 --> 00:10:27,180
So we go ahead and we do a read we go and fetch the thought document and that this comment is a child

124
00:10:27,210 --> 00:10:27,690
of.

125
00:10:27,960 --> 00:10:32,330
And then we get the number of comments from that data.

126
00:10:32,520 --> 00:10:41,730
We decrement the number of comments by one then we get a reference to the comment and we just call transaction

127
00:10:41,940 --> 00:10:43,520
delete document.

128
00:10:43,530 --> 00:10:46,160
All right so when we check this out.

129
00:10:46,350 --> 00:10:48,800
So now it's at 4.

130
00:10:48,930 --> 00:10:50,090
It should be 3.

131
00:10:50,130 --> 00:10:58,650
But if I delete this comment then we should see it be three because it has a decrease the number by

132
00:10:58,650 --> 00:10:59,160
one.

133
00:10:59,190 --> 00:11:00,860
This one has a comment.

134
00:11:00,990 --> 00:11:05,580
I'm going to delete it and it should now be had zero.

135
00:11:05,640 --> 00:11:07,010
And this one has a comment.

136
00:11:07,160 --> 00:11:10,410
I delete it then it's had zero.

137
00:11:10,560 --> 00:11:10,790
All right.

138
00:11:10,800 --> 00:11:13,410
Looks like it is working great for us.

139
00:11:13,410 --> 00:11:19,230
All right so that is how you can delete documents in the fire store if you don't need to worry about

140
00:11:19,230 --> 00:11:20,670
doing other reads and writes.

141
00:11:20,700 --> 00:11:25,830
Then you can call this a simpler function which is just getting a reference to the document and then

142
00:11:25,830 --> 00:11:32,100
calling delete if you need to handle it with along with other reads and writes then you're going to

143
00:11:32,100 --> 00:11:36,150
need to run it in a transaction which is similar.

144
00:11:36,150 --> 00:11:42,500
All you got to do is get the reference to the document and then call transaction delete document.

145
00:11:42,510 --> 00:11:45,730
All right so yeah super useful to know.

146
00:11:45,810 --> 00:11:51,540
Being able to delete data is you know something that you'll use very often in apps over again a lot

147
00:11:51,540 --> 00:11:52,740
of value out of this.

148
00:11:52,770 --> 00:11:58,440
And I will see you all in the next one where we start where we add the ability to edit the comment as

149
00:11:58,440 --> 00:11:58,630
well.

150
00:11:58,640 --> 00:11:59,030
All right.

151
00:11:59,100 --> 00:11:59,880
So I'll see you then.
