0
1
00:00:00,980 --> 00:00:08,580
Up till now, in order to persist data, we've essentially only been using single tables, right?
1

2
00:00:08,590 --> 00:00:14,860
You think about how we save data using UserDefaults or using our plist and decodable protocol,
2

3
00:00:14,990 --> 00:00:20,540
we were still saving things into a single table that had a key-value pair.
3

4
00:00:20,540 --> 00:00:24,280
Now, this is equivalent to your tables inside Excel, for example.
4

5
00:00:24,410 --> 00:00:31,040
So in order to explain the next step of persistent data storage solutions, let's use an example. 
5

6
00:00:31,040 --> 00:00:33,560
Let's say that you own a small bakery,
6

7
00:00:33,560 --> 00:00:33,940
right?
7

8
00:00:34,040 --> 00:00:40,670
And in order to keep track of your orders, your ingredients, your cost, you have to make a table,
8

9
00:00:40,700 --> 00:00:41,230
right?
9

10
00:00:41,270 --> 00:00:46,880
People would probably do this normally inside Excel. And inside the single table, you list all the things
10

11
00:00:46,880 --> 00:00:47,660
that you can make,
11

12
00:00:47,660 --> 00:00:52,760
so you just make cookies, brownies, and cakes. And you have columns where you can write down what are the
12

13
00:00:52,760 --> 00:00:58,670
ingredients, what's the cost for each of these products, and how many orders you're getting, say, every
13

14
00:00:58,670 --> 00:00:59,780
month or every week.
14

15
00:00:59,900 --> 00:01:04,480
And this can be easily done using our plist or using our UserDefaults.
15

16
00:01:04,640 --> 00:01:10,950
But what happens if you know, say, your cookies get featured on the Cookie Magazine, right?
16

17
00:01:10,970 --> 00:01:15,880
And you end up being the Best Chewy Chocolate Chip Cookies in the world,
17

18
00:01:15,890 --> 00:01:16,710
what are you going to do?
18

19
00:01:16,700 --> 00:01:21,890
Well, then your sales are going to massively explode and people are going to be queuing around the block
19

20
00:01:22,010 --> 00:01:24,090
to buy your cookies, right?
20

21
00:01:24,090 --> 00:01:29,120
Now, I just want to take a moment and pause and reflect on the fact that it took me about half an hour to
21

22
00:01:29,120 --> 00:01:35,390
find the perfect cookie image to illustrate the slide and given the fact that I've been trying to cut
22

23
00:01:35,570 --> 00:01:38,770
all sugars in my diet for the last year,
23

24
00:01:39,230 --> 00:01:44,090
I just want you to know how much of a sacrifice I've just made for you, guys,
24

25
00:01:44,180 --> 00:01:48,610
looking at a million chocolate chip cookie images that I can have.
25

26
00:01:49,040 --> 00:01:50,680
But anyways, let's continue.
26

27
00:01:50,750 --> 00:01:56,990
So your business explodes and you now have loads and loads of orders, including big supermarkets and
27

28
00:01:56,990 --> 00:02:01,290
big brands, and they all want to buy your cookies and all of your bakery goods.
28

29
00:02:01,310 --> 00:02:06,740
So your single table inside Excel is not going to cut it anymore because you now need to keep track
29

30
00:02:06,770 --> 00:02:08,110
of a lot more things.
30

31
00:02:08,110 --> 00:02:13,850
So for your products, for example, you need to be able to track the ingredients, the cost, the shelf life
31

32
00:02:13,850 --> 00:02:14,670
of each of them,
32

33
00:02:14,750 --> 00:02:16,340
what are the nutrition details.
33

34
00:02:16,340 --> 00:02:18,900
Because all of these big shops, they all want to know, right?
34

35
00:02:18,920 --> 00:02:22,480
So you have to test that and you have to update the products table.
35

36
00:02:22,550 --> 00:02:25,580
But in addition, you might want to keep track of your buyers, right?
36

37
00:02:25,580 --> 00:02:30,920
Say Walmart, Tesco, and Whole Food, they're all regular buyers of your stuff, and now you need to know what
37

38
00:02:30,920 --> 00:02:34,270
is their address, what are their phone number, what are their bank details,
38

39
00:02:34,340 --> 00:02:36,820
who do you need to contact in order to invoice them,
39

40
00:02:36,860 --> 00:02:40,570
whole bunch of details about your buyers. So you need another table.
40

41
00:02:40,670 --> 00:02:46,120
And then in addition to your products' and your buyers' table, you also need to keep track of your orders,
41

42
00:02:46,130 --> 00:02:46,430
right?
42

43
00:02:46,430 --> 00:02:52,450
So, for example, this month Walmart wanted 2 batches of cookies, Tesco's wanted 12 batches, and Whole
43

44
00:02:52,450 --> 00:02:54,530
Foods wanted 10 batches of your brownies.
44

45
00:02:54,530 --> 00:02:59,660
So you need to match up the items from the Products' table to the buyers' table
45

46
00:02:59,690 --> 00:03:05,040
in order to know who to invoice, who to send which details about which products, a whole bunch of data.
46

47
00:03:05,240 --> 00:03:09,560
And you now have three tables and the tables are related to each other,
47

48
00:03:09,560 --> 00:03:11,410
they have relationships, right?
48

49
00:03:11,480 --> 00:03:16,520
They're not just single tables. Because when you have an order for two batches of cookies to  Walmart,
49

50
00:03:16,730 --> 00:03:21,770
you need to pull out data from the Products' table for the cookies and you need to pull out data from
50

51
00:03:21,770 --> 00:03:24,200
the Buyers' table for Walmart.
51

52
00:03:24,320 --> 00:03:28,050
So you now need to establish relationships between the tables.
52

53
00:03:28,130 --> 00:03:33,680
And this is when you will need a database. And in the coming lessons, we're going to be talking about
53

54
00:03:34,040 --> 00:03:40,700
two database solutions that are way more sophisticated than the single tables that we've been working
54

55
00:03:40,700 --> 00:03:47,160
with using plist and UserDefaults, and they'll allow us to establish relationships between our tables
55

56
00:03:47,160 --> 00:03:54,230
or between our classes so that we can perform far more complex tasks like querying our database to find
56

57
00:03:54,230 --> 00:03:58,850
a particular item or linking two tables together using relationships.
57

58
00:03:58,850 --> 00:04:04,850
Now, let's just quickly review some of the methods that we've come across to persistently store on data,
58

59
00:04:05,030 --> 00:04:06,580
on the local device.
59

60
00:04:06,590 --> 00:04:12,290
So the ones that we've seen and that we've used are user defaults and codable.
60

61
00:04:12,320 --> 00:04:19,790
So the first section here, the ones in blue are essentially tables, the plist, all their individual tables
61

62
00:04:19,790 --> 00:04:21,770
that tracks a small amount of data.
62

63
00:04:21,770 --> 00:04:28,370
Now, the three solutions in yellow are databases or database solutions, and they allow for more complex
63

64
00:04:28,370 --> 00:04:30,570
data and apps with more complex needs
64

65
00:04:30,650 --> 00:04:33,000
in terms of data storage and data retrieval.
65

66
00:04:33,020 --> 00:04:34,750
So let's go from the top.
66

67
00:04:34,850 --> 00:04:41,330
So the first method that we saw and we used in order to persist our data, which is probably the easiest way
67

68
00:04:41,330 --> 00:04:43,390
of doing it, is UserDefaults.
68

69
00:04:43,680 --> 00:04:49,190
And this allows you to quickly persists small bits of data, things like, what is the top score of the user,
69

70
00:04:49,470 --> 00:04:54,640
what does the player's chosen nickname, or whether if you want music on or off.
70

71
00:04:54,710 --> 00:05:01,050
These are really small bits of data and that's what you should use UserDefaults for. Once you start
71

72
00:05:01,050 --> 00:05:05,610
trying to put objects into UserDefaults, then you know that you're probably doing something a little
72

73
00:05:05,610 --> 00:05:06,240
bit wrong.
73

74
00:05:06,370 --> 00:05:13,890
Now, that's when we moved on to the Codable protocol which allows us to save many individual plist that
74

75
00:05:13,890 --> 00:05:22,230
contain our custom objects. So you can see it almost as taking an ice cream and dehydrating and storing
75

76
00:05:22,230 --> 00:05:25,620
it as astronaut ice-cream which taste terrible, by the way.
76

77
00:05:25,620 --> 00:05:30,210
I don't know who came up with this. But it's a good analogy for the item saved with the Codable protocol.
77

78
00:05:30,210 --> 00:05:35,730
You basically flash freezing your custom objects into a plist, and then when you use it, you have to
78

79
00:05:35,730 --> 00:05:42,990
retrieve the entire plist or load the entire plist into memory before you can use any of the items or
79

80
00:05:42,990 --> 00:05:45,030
objects contained within the plist.
80

81
00:05:45,030 --> 00:05:50,970
So, again, it's memory intensive to load up an entire table when you only want one or two items inside
81

82
00:05:50,970 --> 00:05:51,550
it.
82

83
00:05:51,600 --> 00:05:58,650
And this is why the Codable protocol, saving your custom plist to file, again, should only be used for
83

84
00:05:58,650 --> 00:06:00,120
small pieces of data.
84

85
00:06:00,210 --> 00:06:02,640
So less than 100 kilobytes, for example.
85

86
00:06:02,820 --> 00:06:04,930
Then we've got something that we haven't seen yet,
86

87
00:06:05,050 --> 00:06:06,750
and it's called the Keychain.
87

88
00:06:06,960 --> 00:06:13,730
And this is an API that allows us to save, again, small bits of data inside a table, but allows us to save
88

89
00:06:13,730 --> 00:06:19,870
that securely, and Apple takes care of a lot of the heavy lifting of keeping the data that you put into
89

90
00:06:19,890 --> 00:06:21,760
the Keychain secure.
90

91
00:06:21,990 --> 00:06:27,210
So this is with things like, you know, usernames, passwords, things that you probably want to keep secure
91

92
00:06:27,240 --> 00:06:28,170
for the user.
92

93
00:06:28,170 --> 00:06:33,300
And then we crossed the threshold and we move into the database solutions. And the first one that comes
93

94
00:06:33,300 --> 00:06:34,940
to mind is SQLite.
94

95
00:06:35,070 --> 00:06:41,040
So pretty much all of your iPhone apps, their backend or the database that stores the data on the
95

96
00:06:41,040 --> 00:06:46,200
device is mostly using SQLite. And SQLite has been around for a long time.
96

97
00:06:46,290 --> 00:06:53,820
If you're familiar with relational databases like MySQL  or SQL, then you are probably familiar
97

98
00:06:53,820 --> 00:06:54,560
with SQLite.
98

99
00:06:54,600 --> 00:07:02,340
It's meant to be a lightweight, easy-to-use relational database that will help you to persist large amounts
99

100
00:07:02,340 --> 00:07:05,680
of data and it has efficient ways built in
100

101
00:07:05,700 --> 00:07:07,940
in order for you to query data from it.
101

102
00:07:07,980 --> 00:07:13,740
So, for example, when you're trying to sort a SQLite database, you don't have to load up your entire
102

103
00:07:13,740 --> 00:07:18,310
database on your tiny iPhone, and then try to sort everything inside,
103

104
00:07:18,330 --> 00:07:24,440
no, people have come up with really efficient solutions, so that it's quick and it's not so intensive on memory.
104

105
00:07:24,540 --> 00:07:30,930
And if you're familiar, ready with the Querying Language and SQL databases, then this might potentially
105

106
00:07:30,930 --> 00:07:32,930
be a solution that you want to use.
106

107
00:07:33,020 --> 00:07:38,150
Now, the next one in the list is Core Data and this can use SQLite
107

108
00:07:38,310 --> 00:07:39,700
as the database.
108

109
00:07:39,900 --> 00:07:46,810
And whenever people talk about Core Data, you'll hear them hesitate to call it a database.
109

110
00:07:46,860 --> 00:07:53,070
And the reason for that is because while you can use Core Data to work with a relational database, 
110

111
00:07:53,070 --> 00:08:00,030
it's so much more than that. It allows you to convert these individual tables inside your database into objects
111

112
00:08:00,150 --> 00:08:02,490
that you can manipulate easily using code.
112

113
00:08:02,580 --> 00:08:07,440
It gives you efficient methods to query the database using a more natural language.
113

114
00:08:07,440 --> 00:08:10,310
It can also do a lot of things that you can't do with SQLite
114

115
00:08:10,310 --> 00:08:15,030
For example, monitor for changes in the database and a whole lot more.
115

116
00:08:15,210 --> 00:08:18,840
I could probably talk about Core Data by itself for 40 hours.
116

117
00:08:18,840 --> 00:08:23,700
So we're only going to be looking at the most fundamental things that you can do with Core Data which
117

118
00:08:23,700 --> 00:08:27,150
is basically create, read, update, and destroy.
118

119
00:08:27,150 --> 00:08:31,370
And that's the base functionality that you'll need for pretty much any database you want to use,
119

120
00:08:31,620 --> 00:08:33,610
and we're going to look into all of them.
120

121
00:08:33,720 --> 00:08:40,830
Now, the last one in the list is something called Realm, and this is a open-source framework that is a much
121

122
00:08:40,920 --> 00:08:48,120
faster and easier database solution than Core Data and it's fast becoming one of the most popular ways
122

123
00:08:48,210 --> 00:08:51,150
of persisting data locally on device.
123

124
00:08:51,150 --> 00:08:53,520
So we're going to be looking into that as well.
124

125
00:08:53,520 --> 00:09:00,720
So when you're looking to save data from the user inside your apps and have it persist across, you know,
125

126
00:09:00,720 --> 00:09:07,390
app updates or iOS updates, then you should consider maybe one of these six methods.
126

127
00:09:07,410 --> 00:09:12,750
So if you want to persist just a tiny amount of information like a small key-value pair, then consider
127

128
00:09:12,750 --> 00:09:16,840
something like UserDefaults. If you want to save a small amount of custom objects,
128

129
00:09:16,860 --> 00:09:22,680
then think about using Codable and encoding your data into a plist. If you want a safe secure data
129

130
00:09:22,730 --> 00:09:25,780
like user passwords, then use something like Keychain.
130

131
00:09:25,980 --> 00:09:32,130
And if you have a large amount of data and you need to be able to query the data to pull out specific entries
131

132
00:09:32,130 --> 00:09:35,260
inside, then you want to think about one of the database solutions.
132

133
00:09:35,340 --> 00:09:40,290
If you already know about a SQLite or you already have data stored in a SQLite database, then you
133

134
00:09:40,290 --> 00:09:42,250
might want to use SQLite with a wrapper.
134

135
00:09:42,420 --> 00:09:45,690
If you're sending up your data from scratch, then Core Data might be a good solution.
135

136
00:09:45,690 --> 00:09:50,000
There's a lot of help online for Core Data. A lot of people use it.
136

137
00:09:50,040 --> 00:09:52,180
Most iOS developers are familiar with it.
137

138
00:09:52,200 --> 00:09:54,290
And finally, if you want to be able to do,
138

139
00:09:54,480 --> 00:10:00,210
I would say, most things that you can do with Core Data but faster, quicker, and have a more efficient database
139

140
00:10:00,420 --> 00:10:03,070
solution, then you might think about something like Realm.
140

141
00:10:03,210 --> 00:10:09,480
So let's get started looking at, firstly, Core Data, and then looking at Realm, and you can make your own
141

142
00:10:09,480 --> 00:10:11,790
decisions when it comes to creating your own apps.
