1
00:00:05,220 --> 00:00:10,700
Alright, so the content resolver that we started talking about in the end of the last video, will execute

2
00:00:10,700 --> 00:00:16,550
our query against its data source and give us back a cursor. And then we we can then use the cursor just

3
00:00:16,550 --> 00:00:19,010
like we've done in previous videos.

4
00:00:19,010 --> 00:00:23,990
We'll just end up using a loop to loop through all the rows in the cursor, and then to display the contact

5
00:00:23,990 --> 00:00:26,300
names. So that bit's easy,

6
00:00:26,390 --> 00:00:28,800
but what's this ContentResolver all about.

7
00:00:28,820 --> 00:00:30,780
Let's talk about that in a bit more detail.

8
00:00:33,840 --> 00:00:39,440
So to explain about the various classes that are used when we access data using a ContentProvider, let's

9
00:00:39,450 --> 00:00:44,210
consider some cooks who need milk and eggs for whatever they're going to be cooking today.

10
00:00:44,460 --> 00:00:50,140
Now to get their ingredients for their meal, they're going to go to their local shop to buy milk and eggs.

11
00:00:52,550 --> 00:00:55,090
Now the shop uses a number of suppliers.

12
00:00:55,270 --> 00:00:59,620
They may send someone to the farmers market very early in the morning, or the goods might be delivered

13
00:00:59,620 --> 00:01:01,210
by huge trucks overnight

14
00:01:01,570 --> 00:01:03,500
in the case of modern supermarkets.

15
00:01:03,500 --> 00:01:04,760
But however they do it,

16
00:01:04,780 --> 00:01:10,270
they can provide mild and eggs when a customer asks for them.

17
00:01:10,280 --> 00:01:14,750
Now I've simplified this of course, because modern supply chains tend to be quite complex.

18
00:01:14,850 --> 00:01:18,660
But the shop supplies ultimately get their milk and eggs from a farm.

19
00:01:18,900 --> 00:01:24,870
So the slide here shows the flow of food from the source - so the cows and chickens, who are the source of

20
00:01:24,870 --> 00:01:27,380
the milk or the eggs - to the farmers market.

21
00:01:30,440 --> 00:01:37,120
Now the farmers market supplies the shop with the goods, and the shop in turn, passes it on to the customer -

22
00:01:37,220 --> 00:01:38,970
our cooks in this example.

23
00:01:39,020 --> 00:01:43,790
Now I guess the analogy falls down a bit because the milk and eggs only go in one direction; from the

24
00:01:43,790 --> 00:01:48,500
source to the suppliers, then to the shop and finally to the customers.

25
00:01:51,160 --> 00:01:56,110
In the case of data being retrieved from a data source, we can obviously store data in the database as

26
00:01:56,140 --> 00:01:57,480
well as taking it out.

27
00:01:57,790 --> 00:02:03,250
But if we ignore the fact that you wouldn't try to put an egg back into a chicken, this maps quite nicely to what

28
00:02:03,250 --> 00:02:06,540
we're doing with our ContentProvider and ContenResolver.

29
00:02:06,910 --> 00:02:08,949
So let's see how they fit into all of this.

30
00:02:12,160 --> 00:02:12,460
Alright.

31
00:02:12,460 --> 00:02:15,240
So data is requested by a client,

32
00:02:15,360 --> 00:02:22,090
and that's a commonly used term in computing for some class or process that requests either data or a service

33
00:02:22,150 --> 00:02:23,300
from something else.

34
00:02:23,620 --> 00:02:28,090
So here the client is the cooks, and they're requesting ingredients from the shop.

35
00:02:31,380 --> 00:02:36,450
Now in our case, in an Android program, the client may be an activity or a loader.

36
00:02:36,690 --> 00:02:41,710
Now in the example we're working on, the client is MainActivity as that's what's attempting to retrieve

37
00:02:41,710 --> 00:02:42,910
the Contacts data.

38
00:02:45,800 --> 00:02:51,280
Now Main Activity requests the data at once from a ContentResolver, and that's performing the role

39
00:02:51,280 --> 00:02:52,860
of the shop in our diagram.

40
00:02:55,820 --> 00:03:00,890
Now in the same way that the cooks don't have to know where the milks and eggs are coming from, MainActivity -

41
00:03:00,890 --> 00:03:07,780
the client here - doesn't know where the ContentResolver will be fetching the Contacts data from. So Main

42
00:03:07,930 --> 00:03:11,420
Activity specifies the kind of data it wants - Contacts data

43
00:03:11,450 --> 00:03:18,980
in this case - in the same way that the cooks specify whether they want milk or eggs.

44
00:03:19,160 --> 00:03:24,620
So it's the job of the shop to get the milk from wherever it gets milk from, and to get eggs from wherever

45
00:03:24,620 --> 00:03:29,120
it gets eggs from.

46
00:03:29,120 --> 00:03:32,180
In other words they don't have to both come from the same place.

47
00:03:32,180 --> 00:03:38,150
The key thing here is that when the customer asks for milk, the shop knows where milk comes from.

48
00:03:41,350 --> 00:03:44,560
Now if the customer asks for eggs, this is a different request

49
00:03:44,560 --> 00:03:49,870
and the shop needs to go to the egg supplier rather than its milk supplier. In the same way,

50
00:03:49,890 --> 00:03:56,280
it's the job of the ContentResolver to get the Contacts data from whoever, or wherever it needs to get

51
00:03:56,280 --> 00:03:57,370
it from.

52
00:03:57,510 --> 00:04:02,640
Now if we'd asked the ContentResolver for phone log data instead, it would then go off to a different

53
00:04:02,640 --> 00:04:06,410
place - a different supplier or in this case - to get the phone log data.

54
00:04:09,910 --> 00:04:13,460
Where it goes to get the data is to a ContentProvider.

55
00:04:13,660 --> 00:04:19,450
Now we can see here, there are two ContentProviders. One supplies milk and the other suppliers eggs.

56
00:04:20,110 --> 00:04:22,520
So depending on what the customer requests,

57
00:04:22,600 --> 00:04:29,120
the shop can go off to the appropriate supplier to get it.

58
00:04:29,170 --> 00:04:34,180
Now it's the same when MainActivity requests contact data from the ContentResolver.

59
00:04:34,260 --> 00:04:40,230
So MainActivity uses the URI to specify the kind of data that it wants. The Content

60
00:04:40,330 --> 00:04:47,340
Resolver then uses the URI to decide which ContentProvider it should ask to supply the data.

61
00:04:47,360 --> 00:04:50,350
Now we're going to be looking a bit more at the mechanisms behind this.

62
00:04:50,460 --> 00:04:53,110
At the moment though, we're just seeing how it all fits together.

63
00:04:56,140 --> 00:05:01,810
So one important thing here is that the ContentResolver doesn't know where the ContentProvider will

64
00:05:01,810 --> 00:05:07,930
get the data from. And there's no reason why it should, because it just requests the data and expects

65
00:05:07,930 --> 00:05:09,200
it to be delivered.

66
00:05:09,310 --> 00:05:13,760
The actual details of how the data's collected, and from where, is irrelevant.

67
00:05:14,120 --> 00:05:19,510
Now in the same way that the store - or shop - doesn't need to know which farms the suppliers get the

68
00:05:19,510 --> 00:05:20,350
milk and eggs from

69
00:05:24,080 --> 00:05:28,210
Now the ContentProvider fetches the data from its data source.

70
00:05:28,390 --> 00:05:34,330
So the milk ContentProvider goes to the cows to get milk, and the eggs ContentProvider goes to the

71
00:05:34,330 --> 00:05:36,440
chickens for eggs.

72
00:05:36,440 --> 00:05:43,300
In our app, the Contacts ContentProvider goes to the Contacts database to get the contacts records that have

73
00:05:43,300 --> 00:05:46,790
been requested.

74
00:05:46,970 --> 00:05:49,470
So you can see that there's quite a bit of obstruction there -

75
00:05:49,730 --> 00:05:52,090
but there is in modern shopping habits too.

76
00:05:52,540 --> 00:05:56,150
So it's not essential that we have supermarkets and suppliers and so on.

77
00:05:56,300 --> 00:05:59,970
We could all go directly to the farms if we wanted to to get our milk and eggs there.

78
00:06:03,240 --> 00:06:05,940
But if you think about it that would be terribly inefficient, though.

79
00:06:06,140 --> 00:06:11,400
So as populations increased and people started living in cities, we started to get layers of abstraction

80
00:06:11,400 --> 00:06:13,390
added to the way we shop for food.

81
00:06:13,620 --> 00:06:17,600
But going back far enough in time, people may have had their own cow and their own chickens.

82
00:06:20,760 --> 00:06:25,830
But then it became more efficient for some people to look after the animals, while others became blacksmiths

83
00:06:25,830 --> 00:06:27,720
or house-builders or whatever.

84
00:06:30,570 --> 00:06:34,970
So now the blacksmiths and the builders are abstracted from the source of their food.

85
00:06:37,890 --> 00:06:42,880
As the population grew, suppliers collected the milk from farms in the country and supplied the

86
00:06:42,900 --> 00:06:50,950
shops in the towns and cities. so that's more levels of abstraction. Now terms like levels of abstraction

87
00:06:50,950 --> 00:06:54,130
or abstracting data can sound very confusing,

88
00:06:54,130 --> 00:06:59,920
but this is all that they really mean. When the consumer of data is separated from the source of that data,

89
00:07:00,190 --> 00:07:05,950
then that's abstraction. The more things there are between the consumer and the source, the more levels

90
00:07:06,370 --> 00:07:08,710
of abstraction there are.

91
00:07:09,040 --> 00:07:14,380
So that's how the ContentResolver fits into our attempt to access the Contacts data.

92
00:07:17,590 --> 00:07:23,140
Now the Content Resolver provides a layer of abstraction between our app, which is the client, and the

93
00:07:23,140 --> 00:07:29,500
Content Provider. The Content Provider adds another layer of abstraction on top of the underlying

94
00:07:29,500 --> 00:07:30,560
data source.

95
00:07:34,100 --> 00:07:39,650
An application has a single ContentResolver that provides access to all the ContentProviders

96
00:07:39,650 --> 00:07:42,260
that exist on the Android device.

97
00:07:42,260 --> 00:07:48,020
And that's why we used getContentResolver rather than creating a new one, because there is only one, and

98
00:07:48,020 --> 00:07:52,460
it holds a list of all the Content Providers that are registered on the device.

99
00:07:55,430 --> 00:08:01,280
Now the Content Resolver uses something called an Authority that's included in the URI to decide

100
00:08:01,280 --> 00:08:05,150
which Content Provider to direct data requests to.

101
00:08:05,480 --> 00:08:09,950
Now we'll have a look at what an Authority is when we create our own Content Provider later.

102
00:08:09,950 --> 00:08:15,410
The important thing is that there's a mechanism that the client can use to let the Content Resolver

103
00:08:15,410 --> 00:08:21,400
know what it wants, and the Content Resolver uses that to decide where to go for the data.

104
00:08:23,920 --> 00:08:25,930
Alright so that's how it all fits together.

105
00:08:25,950 --> 00:08:27,610
I'm going to finish this video here now.

106
00:08:27,680 --> 00:08:31,970
In the next video let's go back and finish off the code and retrieve the contacts, now that we've got

107
00:08:31,970 --> 00:08:37,490
a good understanding of, hopefully got a good understanding of the Content Provider and also how this Content

108
00:08:37,490 --> 00:08:38,580
Resolver works.

109
00:08:38,809 --> 00:08:40,150
So I'll see you in the next video.

