1
00:00:08,050 --> 00:00:13,390
Hey everybody this is Caleb with Dev slopes and in this video we're gonna be talking about core data.

2
00:00:13,420 --> 00:00:17,800
Obviously it's the most important part of this section but we're going to be talking about what even

3
00:00:17,800 --> 00:00:22,630
is core data and how does it work so that when we use that we understand what's going on behind the

4
00:00:22,630 --> 00:00:23,480
scenes.

5
00:00:23,500 --> 00:00:29,800
So to begin I just want to show you this graphic that I've built for us to help explain the concept

6
00:00:29,800 --> 00:00:31,720
of the core data stack.

7
00:00:31,840 --> 00:00:39,040
K so what you need to know is that the core data stack contains three main objects the managed object

8
00:00:39,040 --> 00:00:44,890
model the managed object context and the persistent store coordinator.

9
00:00:44,890 --> 00:00:49,900
Now if you know anything about core data already you know that its purpose is for persistence storage

10
00:00:50,200 --> 00:00:55,630
we can store things with core data and then access them later without them being destroyed every time

11
00:00:55,630 --> 00:01:02,560
that our app is closed or quit or if our phone restarts the core data allows us to persistently store

12
00:01:02,560 --> 00:01:04,230
data it's really really cool.

13
00:01:04,670 --> 00:01:10,180
Now to begin every core data app has a managed object model.

14
00:01:10,350 --> 00:01:17,460
OK now what the managed object model does is it pulls our data model into the core data stack.

15
00:01:17,560 --> 00:01:21,870
So it basically takes our data model and pulls it into the core data stack.

16
00:01:21,880 --> 00:01:26,230
Now the managed object context is what's going to do all of the work.

17
00:01:26,230 --> 00:01:30,890
And as a developer you're actually going to be interacting with this most of the time.

18
00:01:31,050 --> 00:01:31,530
OK.

19
00:01:31,690 --> 00:01:36,580
And when you use core data in general you're going to be dealing with the managed object context.

20
00:01:36,730 --> 00:01:43,120
OK now something you need to know is that the managed object context keeps a reference to the persistent

21
00:01:43,120 --> 00:01:46,730
store coordinator and I'm going to explain why that's important in a moment.

22
00:01:46,780 --> 00:01:53,440
But as you would expect the persistent store coordinator coordinates the persistent storage of data

23
00:01:53,440 --> 00:02:00,940
in our app K and it basically holds a persistent store that has all of our information that we save

24
00:02:00,970 --> 00:02:03,360
and want to retrieve later on.

25
00:02:03,370 --> 00:02:10,390
Now what this does is it understands our data model and it manages storing that information persistently.

26
00:02:10,520 --> 00:02:11,050
OK.

27
00:02:11,410 --> 00:02:12,510
Very very cool.

28
00:02:12,730 --> 00:02:20,110
Now to create a core data stack what we first need to do is to instantiate the data model which is used

29
00:02:20,170 --> 00:02:23,780
to create the managed object model in our core data stack.

30
00:02:23,900 --> 00:02:24,720
OK.

31
00:02:24,970 --> 00:02:31,670
Now the managed object model is required to instantiate the persistent store coordinator.

32
00:02:31,690 --> 00:02:32,400
OK.

33
00:02:32,710 --> 00:02:38,140
Now the persistent store coordinator first needs to understand the data model.

34
00:02:38,140 --> 00:02:40,100
That's why this comes second.

35
00:02:40,150 --> 00:02:45,730
The object model is created first and then because that's created the persistent store coordinator can

36
00:02:45,730 --> 00:02:46,610
be created.

37
00:02:46,690 --> 00:02:52,270
It needs to understand the model and make sure that our persistent storage is compatible with it.

38
00:02:52,300 --> 00:02:58,280
If our if our object or a far managed object model were to change and suddenly be incompatible with

39
00:02:58,280 --> 00:03:03,740
the persistent store it needs to know that that won't work and then throw the proper error.

40
00:03:03,940 --> 00:03:04,630
OK.

41
00:03:04,870 --> 00:03:10,960
Now what happens is the persistent store coordinator creates a persistent store once it knows that it's

42
00:03:10,960 --> 00:03:11,940
compatible.

43
00:03:12,100 --> 00:03:16,670
And that's why it needs to reference the managed object model because it's kind of a way to verify and

44
00:03:16,720 --> 00:03:21,670
I'm sure you're noticing that there are kind of steps and prerequisites in order to get up to the top

45
00:03:21,670 --> 00:03:23,990
of the core data stack where we interact with it.

46
00:03:24,070 --> 00:03:29,150
And this is for safety and also just to make sure that everything works as smoothly as possible.

47
00:03:29,170 --> 00:03:33,490
Now our application works with the managed object context.

48
00:03:33,580 --> 00:03:34,300
OK.

49
00:03:34,420 --> 00:03:40,000
And it's basically going to access the managed object context and the object context is sort of like

50
00:03:40,000 --> 00:03:45,590
the brain of core data because it manages our collection of managed objects.

51
00:03:45,630 --> 00:03:46,330
OK.

52
00:03:46,480 --> 00:03:52,630
If the managed object context needs to load data from persistent storage where all of our data is saved

53
00:03:53,320 --> 00:03:59,050
basically it asks the persistent store coordinator for it and then the well I'll draw a little arrow

54
00:03:59,050 --> 00:03:59,250
here.

55
00:03:59,260 --> 00:04:04,900
The managed object context asks the persistent store coordinator if the persistent store is compatible

56
00:04:04,900 --> 00:04:09,910
with our data model it will return the data that we are asking for which is really neat if it needs

57
00:04:09,910 --> 00:04:13,600
to save data like let's say that our app changes some data.

58
00:04:13,600 --> 00:04:19,120
The managed object context we'll pass that into the persistent storage coordinator and then that tells

59
00:04:19,120 --> 00:04:24,610
the persistent store to change and to be modified then it stores it persistently and we can access it

60
00:04:24,640 --> 00:04:26,790
or save to it as much as we want.

61
00:04:26,800 --> 00:04:28,390
It's really really cool.

62
00:04:28,390 --> 00:04:35,260
So as you can see all of this is just a big ladder sort of and there are prerequisites in order to instantiate

63
00:04:35,410 --> 00:04:42,460
each of these elements so we create a data model which is pulled into the managed object model k then

64
00:04:42,760 --> 00:04:48,370
that is used to instantiate the persistent store coordinator which then checks to make sure that the

65
00:04:48,370 --> 00:04:51,370
persistent store is compatible with our data model.

66
00:04:51,370 --> 00:04:57,760
If it is then the managed object context can pull that data and displayed in our app if we want to save

67
00:04:57,760 --> 00:05:03,710
data we can push that back down through the managed object context and then into the persistent store.

68
00:05:03,820 --> 00:05:09,420
This is how core data works as kind of a an umbrella kind of an overview of how it works and of course

69
00:05:09,840 --> 00:05:15,090
as always with any of these topics you can always dive in deeper research deeper and find out so much

70
00:05:15,090 --> 00:05:17,780
more about how this amazing technology works.

71
00:05:17,790 --> 00:05:21,090
Apple has done an incredible job giving us core data.

72
00:05:21,120 --> 00:05:27,720
And so I'm really excited to teach you how this works and how we can basically use all of these to build

73
00:05:27,720 --> 00:05:28,230
an amazing

74
00:05:31,180 --> 00:05:36,400
So let's go ahead and let's move on to the next video where we're actually going to create our first

75
00:05:36,400 --> 00:05:40,910
Chordata data entity and create some core data attributes as well.

76
00:05:40,930 --> 00:05:45,280
We're going to talk about the core data model and how we can use it as a developer in x.

77
00:05:45,280 --> 00:05:48,430
So let's head on over to that video and let's build that now.
