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