0
1
00:00:00,660 --> 00:00:06,690
Now, when you look at most to-do list apps out there, it's very rare that you'll come across a to-do list
1

2
00:00:06,690 --> 00:00:13,530
that only has a single list. But, unfortunately, that's currently all that we've got. Most to-do list apps
2

3
00:00:13,640 --> 00:00:15,300
will have categories.
3

4
00:00:15,300 --> 00:00:22,470
So, for example, cleaning, bills, miscellaneous, grocery, and you'll be able to tap on them to view those
4

5
00:00:22,470 --> 00:00:23,050
lists,
5

6
00:00:23,070 --> 00:00:29,400
and this allows us to classify and categorize so that we can organize our to-do list items in a more
6

7
00:00:29,400 --> 00:00:30,600
logical way.
7

8
00:00:30,810 --> 00:00:36,390
So that's also a good feature for us to add to our Todoey app. And in order to implement it,
8

9
00:00:36,420 --> 00:00:41,260
we'll get to learn all about relationships and how we set them up using Core Data.
9

10
00:00:41,340 --> 00:00:45,970
So the first thing I'm going to do is I'm going to make some UI changes. At the moment,
10

11
00:00:45,990 --> 00:00:52,330
we've got a single table view controller that is embedded inside a navigation controller.
11

12
00:00:52,380 --> 00:00:58,680
Now, in order for us to have a category screen, then we need to drag on another table view controller.
12

13
00:00:58,800 --> 00:01:06,470
So I'm going to place that between the navigation controller and our to-do list items table view controller.
13

14
00:01:06,510 --> 00:01:12,360
Then I'm going to delete this link between the navigation controller and the to-do List controller because
14

15
00:01:12,450 --> 00:01:15,490
this link specifies which is the
15

16
00:01:15,510 --> 00:01:21,210
root view controller, i.e., if you have a navigation stack like a stack of cards, what is the controller that's
16

17
00:01:21,270 --> 00:01:26,850
at the bottom of the stack? And when you say something like pop to root view controller, then that's equivalent to
17

18
00:01:26,850 --> 00:01:30,220
the first screen that's embedded in the navigation stack.
18

19
00:01:30,240 --> 00:01:37,230
So let's go ahead and click on this link and delete it. And then I'm going to replace this category view
19

20
00:01:37,230 --> 00:01:43,560
controller into a navigation controller and our to-do list view controller, and then I'm going to click
20

21
00:01:43,560 --> 00:01:44,950
on the navigation controller,
21

22
00:01:45,180 --> 00:01:51,120
so I get this little yellow button here, I'm going to control drag it to point it towards our brand-new
22

23
00:01:51,120 --> 00:01:58,290
table view controller. And instead of using show or creating a segue, instead, I'm going to create a relationship
23

24
00:01:58,390 --> 00:02:05,820
segue and specify that this is now the root view controller because we first have to see the category
24

25
00:02:05,820 --> 00:02:06,530
table view,
25

26
00:02:06,780 --> 00:02:12,960
and only then can we select one of the categories to take us to all the items that are classified under
26

27
00:02:12,960 --> 00:02:14,020
that category.
27

28
00:02:14,040 --> 00:02:21,510
So that means we also need to set up a segue between our category and our to-do list items by control
28

29
00:02:21,510 --> 00:02:22,270
dragging.
29

30
00:02:22,410 --> 00:02:25,440
And in this case, we're going to go for a show segue.
30

31
00:02:25,650 --> 00:02:31,320
And if you remember every single time we set up a segue, we also have to specify an identifier.
31

32
00:02:31,320 --> 00:02:37,770
So we'll just call this goToItems. And we currently now have this free-floating table view because
32

33
00:02:37,770 --> 00:02:41,420
it doesn't have a view controller class that's associated with it.
33

34
00:02:41,430 --> 00:02:46,750
So let's go ahead and create a new one. And instead of creating a new Swift File, we want a new Cocoa
34

35
00:02:46,770 --> 00:02:47,860
Touch Class.
35

36
00:02:47,940 --> 00:02:48,910
So hit next.
36

37
00:02:49,110 --> 00:02:54,630
And this is going to be a subclass of UITableViewController because we want all of that freebie
37

38
00:02:54,630 --> 00:02:55,910
code that comes with it.
38

39
00:02:56,280 --> 00:03:02,010
And then we're going to call this class the CategoryViewController, make sure language is selected to the Swift,
39

40
00:03:02,490 --> 00:03:03,930
and hit Next.
40

41
00:03:04,320 --> 00:03:10,890
And then we're going to save it inside the Controllers folder or group and making sure that it targets
41

42
00:03:11,020 --> 00:03:17,100
our Todoey app, and then we're going to get Create, and we now have a CategoryViewController. So, I'm actually
42

43
00:03:17,100 --> 00:03:22,050
going to drag that on top so that it makes more sense because we first go to Category, then we go to the
43

44
00:03:22,050 --> 00:03:24,150
to-do list for each category.
44

45
00:03:24,160 --> 00:03:31,260
So, now if we go back to our Main.storyboard, we can specify the Swift file that is going to be responsible
45

46
00:03:31,260 --> 00:03:33,910
for managing this particular view.
46

47
00:03:33,960 --> 00:03:40,500
So if you don't do this step and you head over to the assistant editor, when you select the to-do list
47

48
00:03:40,530 --> 00:03:44,010
item view control, you see the under automatic,
48

49
00:03:44,070 --> 00:03:46,530
we've got TodoListViewController.swift.
49

50
00:03:46,740 --> 00:03:52,490
But when you select this new table you control that currently is not associated with any Swift class,
50

51
00:03:52,650 --> 00:03:59,730
then you can see that under Automatic, you only get a UITableViewcontroller file, and this is also not
51

52
00:03:59,760 --> 00:04:00,320
a Swift file.
52

53
00:04:00,320 --> 00:04:02,600
This is actually an Objective-C header file.
53

54
00:04:02,760 --> 00:04:07,340
So that's not what we want because we can't link up our outlets and we can't do anything,
54

55
00:04:07,410 --> 00:04:11,360
so we have to specify the identity of this
55

56
00:04:11,370 --> 00:04:15,900
view controller. And inside the custom class, we're going to say
56

57
00:04:15,990 --> 00:04:17,780
CategoryViewController and hit enter.
57

58
00:04:17,820 --> 00:04:25,510
So, now automatically, our assistant editor has updated to the corresponding class with our view controller.
58

59
00:04:25,530 --> 00:04:32,700
Before we leave the Main.storyboard is we want to give this prototype cell a identifier. So select the
59

60
00:04:32,700 --> 00:04:36,600
prototype cell, either on the canvas or select in the document outline,
60

61
00:04:36,600 --> 00:04:40,890
but we're going to call it CategoryCell, and hit enter.
61

62
00:04:41,100 --> 00:04:51,060
So, now that we've got our view and our class linked up, we can now create a add button or Bar Button Item
62

63
00:04:51,180 --> 00:04:51,820
rather,
63

64
00:04:52,470 --> 00:04:56,250
and we can drag it to the top right corner of this new
64

65
00:04:56,310 --> 00:04:57,150
view controller.
65

66
00:04:57,210 --> 00:05:03,020
And, again, this is going to be a system item of Add and we're going to change the tint to white.
66

67
00:05:03,020 --> 00:05:09,690
This is going to allow us to add new categories to this table view so that we can organize our to-do list.
67

68
00:05:09,740 --> 00:05:15,200
And once you've done that, we're going to control drag over to our CategoryViewController.swift file
68

69
00:05:15,200 --> 00:05:23,240
and we're going to create a new IBAction, and this IBAction is going to be called add ButtonPressed
69

70
00:05:23,390 --> 00:05:30,130
and it's going to be of type UIBarButtonItem, and hit enter.
70

71
00:05:30,390 --> 00:05:35,430
And the last thing I'm going to do here is select the navigation item here and I'm going to give it
71

72
00:05:35,490 --> 00:05:45,120
a title called Todoey, and then I'm going to change the title here to Items, and that is all we need to
72

73
00:05:45,120 --> 00:05:45,370
do
73

74
00:05:45,390 --> 00:05:47,140
inside the Main.storyboard.
74

75
00:05:47,310 --> 00:05:52,620
So let's head over to the CategoryViewController. And the first thing I want to do is when you set up
75

76
00:05:52,650 --> 00:05:57,810
a new Cocoa Touch Class, that subclasses UITableView, you get a whole bunch of free code, but you also
76

77
00:05:57,810 --> 00:05:59,680
get a lot of comments.
77

78
00:05:59,830 --> 00:06:04,810
And you know we're now pro enough with table views to not need all of this help and guidance.
78

79
00:06:04,890 --> 00:06:11,630
So I'm going to delete all of this code up until viewDidLoad, and then I'm also going to delete all
79

80
00:06:11,640 --> 00:06:14,690
the comments inside viewDidLoad. That cleans up.
80

81
00:06:14,970 --> 00:06:20,270
view controller a little bit, and allows us to work on it without all of this distraction.
81

82
00:06:20,280 --> 00:06:26,040
So, now that we've set up our view and our controller, we're going to go and figure out what we're going
82

83
00:06:26,040 --> 00:06:27,390
to do with our data model.
83

84
00:06:27,390 --> 00:06:34,650
So up till now, we've been editing our entities and attributes under this table view format, but you can also
84

85
00:06:34,650 --> 00:06:41,990
switch to the graph view and this presents each entity and all of the attributes as a little table.
85

86
00:06:42,060 --> 00:06:47,480
And for those guys who've been working with databases, then this might seem quite familiar to you.
86

87
00:06:47,580 --> 00:06:54,420
So it's under this format that we're going to add a new entity, and this new entity is going to be called
87

88
00:06:54,540 --> 00:06:55,730
Category.
88

89
00:06:55,980 --> 00:07:00,060
And once you hit enter, then you'll see this new table show up
89

90
00:07:00,060 --> 00:07:03,850
that's called Category which has no attributes and no relationship.
90

91
00:07:03,870 --> 00:07:10,670
If you select the Category entity and click Add Attribute, then you're going to get a new attribute.
91

92
00:07:11,040 --> 00:07:17,520
And we can click on it to modify its properties inside the Data Model Inspectah in the right-hand pane
92

93
00:07:17,520 --> 00:07:18,100
here.
93

94
00:07:18,360 --> 00:07:25,680
Now, for some reason I have no idea why the icon for it is like a little welding mask, but some designer
94

95
00:07:25,680 --> 00:07:30,780
somewhere felt that this was a good idea to represent a data model. But, hey, let's not argue with Apple
95

96
00:07:30,780 --> 00:07:31,900
designers, right?
96

97
00:07:32,230 --> 00:07:36,750
Okay, so we're going to change the attribute name to be called name.
97

98
00:07:36,960 --> 00:07:42,510
And this is going to refer to the name of each category that we're going to add, and we're going to make
98

99
00:07:42,510 --> 00:07:44,370
the name nonoptional.
99

100
00:07:44,490 --> 00:07:50,190
So it has to have a value for it to go into our data model. And you'll see this little error here and it won't
100

101
00:07:50,190 --> 00:07:54,080
go away until you change the attribute type from Undefined.
101

102
00:07:54,090 --> 00:07:55,420
It cannot be undefined.
102

103
00:07:55,530 --> 00:08:01,110
It must be specified. And our name for the category is, of course, going to be a string.
103

104
00:08:01,110 --> 00:08:05,190
And once you do that, then Xcode is all happy with you, and all the errors go away.
104

105
00:08:05,190 --> 00:08:12,300
Now, in addition to having a single attribute for the category, we're also now going to set up a relationship
105

106
00:08:12,300 --> 00:08:16,280
between our category and our item entities.
106

107
00:08:16,470 --> 00:08:23,820
And to do that, all you need to do is control drag from Category to Item, and now you have a relationship.
107

108
00:08:23,880 --> 00:08:28,560
Now, it's not very useful having a relationship called new Relationship.
108

109
00:08:28,560 --> 00:08:30,270
So we need to rename that.
109

110
00:08:30,270 --> 00:08:36,540
So click on it and that will allow us to edit it in the Data Model Inspector. We're going to change the
110

111
00:08:36,540 --> 00:08:38,880
name to something that makes sense.
111

112
00:08:38,880 --> 00:08:43,540
So each category can be associated with many items, right?
112

113
00:08:43,560 --> 00:08:49,200
If you have a category called shopping list, then you might have 10 item objects that refer to all the
113

114
00:08:49,200 --> 00:08:50,310
things you need to buy.
114

115
00:08:50,490 --> 00:08:56,280
So we can call this relationship Items because that's essentially what it's referring to.
115

116
00:08:56,400 --> 00:09:04,920
And we should change the type from To One to To Many because each category can have many items associated
116

117
00:09:04,920 --> 00:09:05,670
with it.
117

118
00:09:05,670 --> 00:09:12,990
Now, the inverse relationship refers to what you call the Category that the Item is associated with, and
118

119
00:09:12,990 --> 00:09:21,240
that should probably be called the parentCategory, and each item can only have one parentCategory.
119

120
00:09:21,240 --> 00:09:28,480
So this type should remain as To One. It will be really confusing if each of your items have two categories,
120

121
00:09:28,560 --> 00:09:34,830
say, you know, your "buy milk" belongs in shopping list, as well as another category. That doesn't make
121

122
00:09:34,890 --> 00:09:36,630
any sense, logically.
122

123
00:09:36,630 --> 00:09:43,710
So we've now created a new entity called Category and we've also started creating our graph which specifies
123

124
00:09:43,740 --> 00:09:49,860
how categories relate to Items. And this might be a good time to remind yourself of when we first started
124

125
00:09:49,860 --> 00:09:55,560
talking about databases in this module, when we are talking about our bakery having a product's table,
125

126
00:09:56,130 --> 00:10:02,280
a buyer's table, and an order's table. And in order to manage all of the complex data, we need to set up
126

127
00:10:02,280 --> 00:10:03,560
these relationships.
127

128
00:10:03,570 --> 00:10:12,150
So, now when we create a new Category, we can link it with many Item and as managed objects. And every
128

129
00:10:12,150 --> 00:10:17,440
single item object, we can look up its parent category.
129

130
00:10:17,550 --> 00:10:24,150
So this now gives us access to more complex things that we can do with our data including setting up
130

131
00:10:24,150 --> 00:10:30,340
this new view controller and having a whole bunch of categories that each have their individual to-do lists.
131

132
00:10:30,340 --> 00:10:37,870
Now, all we need to do is head back to our CategoryViewController and we need to set up some code to
132

133
00:10:37,870 --> 00:10:39,550
use our data model.
133

134
00:10:39,760 --> 00:10:51,460
So in order to do that, we'll need to import Core Data and we'll also need to have some TableView 
134

135
00:10:51,480 --> 00:11:08,050
DataSource methods, as well as TableView Delegate Methods, and then we'll need some Data Manipulation Methods.
135

136
00:11:08,230 --> 00:11:10,190
So I think you're probably getting the hint.
136

137
00:11:10,270 --> 00:11:12,760
I'm not going to do this for you.
137

138
00:11:12,760 --> 00:11:20,440
I want you, as a challenge, to think back to what we did inside our TodoList View Controller, how we
138

139
00:11:20,440 --> 00:11:27,070
set up an array of items, how we tapped into the context that we're going to use, and then how we set
139

140
00:11:27,070 --> 00:11:35,320
up our tableView to load our data from the context for all of those items. And then we used our 
140

141
00:11:35,320 --> 00:11:40,030
IBAction addButtonPressed to add new items into our tableView.
141

142
00:11:40,420 --> 00:11:46,930
And then we used the saveItems function to persist the items inside our container, and then we loaded
142

143
00:11:46,930 --> 00:11:51,280
those items from our persistent container using our context.
143

144
00:11:51,280 --> 00:11:59,500
Now, I want you to complete this challenge and be able to set up our CategoryViewController in a similar
144

145
00:11:59,500 --> 00:12:07,540
way to what we did with items to use our Core Data data model and to be able to add new categories and
145

146
00:12:07,540 --> 00:12:10,710
to be able to show those categories inside our table.
146

147
00:12:10,960 --> 00:12:16,600
So all you need to do is set up the data source so that we can display all the categories that are inside
147

148
00:12:16,600 --> 00:12:18,050
our persistent container.
148

149
00:12:18,280 --> 00:12:24,840
Set up the Data Manipulation Methods, namely, save data and load data so that we can use CRUD.
149

150
00:12:25,240 --> 00:12:33,190
And finally, to set up the addButtonPressed IBAction in order to Add New Categories using our category
150

151
00:12:33,220 --> 00:12:38,890
entity. And the part that I want you to leave for now is the TableViewDelegate methods
151

152
00:12:38,920 --> 00:12:44,950
So that refers to what should happen when we click on one of the cells inside the category table.
152

153
00:12:45,130 --> 00:12:48,480
So I know that these challenges are getting more and more challenging.
153

154
00:12:48,610 --> 00:12:52,900
But if you've come this far and you followed along with everything that we've been talking about with
154

155
00:12:52,920 --> 00:13:00,070
Core Data, this is a good time to really test to see if you understood the concepts and use what you learned
155

156
00:13:00,190 --> 00:13:07,040
to set up your very own table view controller that has a persistent data store using Core Data.
156

157
00:13:07,150 --> 00:13:12,790
And if you have completed the challenge successfully, then you should end up with a blank table view
157

158
00:13:13,270 --> 00:13:20,560
with the title Todoey, and you should be able to click on the Add button to add new categories to your
158

159
00:13:20,560 --> 00:13:21,330
table view,
159

160
00:13:21,460 --> 00:13:25,330
and when you double click on the home button and terminate your app.
160

161
00:13:25,360 --> 00:13:31,840
Now, I really, really recommend you to give it a go yourself. And it might require quite a bit of struggling,
161

162
00:13:31,870 --> 00:13:33,580
but struggle is good.
162

163
00:13:33,610 --> 00:13:38,500
And once you struggle through it and get it to work by yourself, then you'll feel this immense sense
163

164
00:13:38,500 --> 00:13:39,500
of achievement.
164

165
00:13:39,760 --> 00:13:46,290
But if you do get stuck and you do have any issues, then you can always head over to the next lesson
165

166
00:13:46,660 --> 00:13:49,320
where I will walk you through how we can do this.
166

167
00:13:49,360 --> 00:13:52,200
So pause the video and give it a go now.
167

168
00:13:52,210 --> 00:13:54,420
I'll see you on the other side.
