1
00:00:07,930 --> 00:00:12,830
Hey buddy this is Caleb with slopes and in this video we're going to build our data service.

2
00:00:12,850 --> 00:00:18,290
It's a singleton class that we're going to use to interface with our firebase databased.

3
00:00:18,310 --> 00:00:19,650
Very very cool.

4
00:00:19,660 --> 00:00:25,240
So let's go ahead and pull open our tax code project and what we're going to do to begin is to create

5
00:00:25,240 --> 00:00:27,700
a new Swift file in our services group.

6
00:00:27,700 --> 00:00:33,520
So right click on services click new file and create a new Swift file.

7
00:00:33,520 --> 00:00:38,620
Now of course we're going to name this data service and make sure that you are adding it to the proper

8
00:00:38,620 --> 00:00:41,240
target and click Create.

9
00:00:41,260 --> 00:00:47,740
Now inside the data service we need to create a class just like you might expect by typing class data

10
00:00:47,740 --> 00:00:49,040
service.

11
00:00:49,450 --> 00:00:56,370
Then to make it a singleton class meaning to make it accessible to any other class throughout the life's

12
00:00:56,740 --> 00:01:02,710
lifecycle of this application go ahead and create a static variable static or static constant actually

13
00:01:02,710 --> 00:01:05,580
static let and then call it instance.

14
00:01:05,620 --> 00:01:11,020
And we're going to just instantiate data service so basically we're creating an instance of this view

15
00:01:11,020 --> 00:01:14,200
control of this class inside itself.

16
00:01:14,200 --> 00:01:14,720
Very cool.

17
00:01:14,740 --> 00:01:15,810
Not too bad.

18
00:01:15,850 --> 00:01:20,150
Now what we need to do is we need to set up a few different things.

19
00:01:20,260 --> 00:01:22,870
But before we do that we need to import firebase.

20
00:01:22,870 --> 00:01:25,060
So go ahead and do that.

21
00:01:25,300 --> 00:01:31,700
And now we can actually communicate with firebase now that we've imported it which is very cool.

22
00:01:31,990 --> 00:01:37,420
OK so now what we're going to do is we're going to go ahead and create a constant outside of our class

23
00:01:37,420 --> 00:01:38,850
called D-B base.

24
00:01:38,860 --> 00:01:46,180
Go ahead and type let DBI bass in all caps and we're going to set this to basically allow us to access

25
00:01:46,240 --> 00:01:49,720
our database like the base of our database.

26
00:01:49,720 --> 00:01:53,290
To do that go ahead and type database database.

27
00:01:53,350 --> 00:01:57,550
That means the primary database and then dot reference.

28
00:01:57,720 --> 00:01:58,310
OK.

29
00:01:58,600 --> 00:02:05,860
And if you look this is a function that gets a F-I our database reference for the root of your firebase

30
00:02:05,860 --> 00:02:06,940
database.

31
00:02:06,940 --> 00:02:14,110
Now if I take you over here to firebase I went into our project I went into the database and you'll

32
00:02:14,110 --> 00:02:16,770
notice right here there's a u r l.

33
00:02:16,900 --> 00:02:21,760
This is the base you are l of our database and I'll show you how the rest of it works in a second but

34
00:02:21,760 --> 00:02:22,780
that's what you need to know.

35
00:02:22,870 --> 00:02:28,510
This u r l is what we're going to use for our base you are al of our server.

36
00:02:28,510 --> 00:02:34,570
So this is instantiated outside of our class so that it's accessible to us in the class because this

37
00:02:34,570 --> 00:02:39,570
class by itself doesn't have any initialiser So we're initializing it outside of the class on purpose.

38
00:02:39,580 --> 00:02:43,240
So now we're going to go ahead and make for private variables.

39
00:02:43,240 --> 00:02:51,880
One variable that's going to hold the value of D-B base one that's going to hold a reference to a user's

40
00:02:51,970 --> 00:02:55,450
group or in firebase we call them a child.

41
00:02:55,470 --> 00:03:00,670
So inside of our base you are l if we add a child it means we're adding a group we're like a subgroup

42
00:03:00,700 --> 00:03:03,620
things like nested folders kind of all show you later.

43
00:03:03,760 --> 00:03:11,020
But for now we need to know we're going to create a variable for our base you are l the L for the users

44
00:03:11,170 --> 00:03:15,670
that we're going to store because if you look at our simulator here we need to basically be able to

45
00:03:15,670 --> 00:03:22,390
hold information for all the users all the emails and all of the account types for the groups we need

46
00:03:22,390 --> 00:03:27,730
to hold all of the group information as well as all the messages in each group.

47
00:03:27,730 --> 00:03:31,840
We also need to be able to hold all of the information from the feed so we're going to make for different

48
00:03:31,840 --> 00:03:36,980
variables and we're going to use those to store those references to the firebase database.

49
00:03:37,000 --> 00:03:40,530
So let's go ahead and do that and we're going to use data hiding as well.

50
00:03:40,630 --> 00:03:46,330
So that basically we can only set these values from inside the data service so that nobody else can

51
00:03:46,330 --> 00:03:52,380
access them no other class or no other method can access these except for the data service itself.

52
00:03:52,420 --> 00:03:53,960
Good way to be a safe coder.

53
00:03:53,980 --> 00:04:00,070
So go ahead and type private var setting it to be a private variable and we're going to use underscore

54
00:04:00,580 --> 00:04:04,310
ref base that's going to be equal to D-B base.

55
00:04:04,480 --> 00:04:10,390
OK so that's our database and we're going to create an accessory to set the value of this in just a

56
00:04:10,390 --> 00:04:10,750
second.

57
00:04:10,750 --> 00:04:13,860
But for now we're going to just leave it as D-B base.

58
00:04:13,900 --> 00:04:24,910
Now we're going to do another one here private var ref users and this is going to be used to save our

59
00:04:24,910 --> 00:04:28,480
users into their own sort of folder in firebase.

60
00:04:28,480 --> 00:04:36,220
Now to do that we're going to basically just append a child to D-B base like this D.B base child and

61
00:04:36,220 --> 00:04:39,610
the path string is going to be users.

62
00:04:39,760 --> 00:04:40,420
OK.

63
00:04:40,630 --> 00:04:48,160
Now the cool thing about firebase is if a value does not exist when you go to try to save something

64
00:04:48,160 --> 00:04:53,500
to a certain child if it doesn't already exist it will create it thus preventing crashes.

65
00:04:53,500 --> 00:04:54,640
It's pretty amazing.

66
00:04:54,640 --> 00:04:59,380
So I'll show you how that works as well later but for now just think if this is like we're creating

67
00:04:59,380 --> 00:05:02,700
a folder to hold all the users can let's do it again.

68
00:05:02,710 --> 00:05:15,700
Private var ref groups equals D-B base dot child and the path string is.

69
00:05:15,700 --> 00:05:16,450
Groups.

70
00:05:16,470 --> 00:05:22,520
OK just like users and then go ahead and type private var REF field.

71
00:05:22,800 --> 00:05:26,940
That's to all caps equals D-B base child.

72
00:05:27,000 --> 00:05:29,490
And the path string is pheed.

73
00:05:29,770 --> 00:05:30,490
OK.

74
00:05:30,730 --> 00:05:31,540
Very very cool.

75
00:05:31,540 --> 00:05:33,010
So these are private variables.

76
00:05:33,010 --> 00:05:38,170
Now we're going to create public variables in order to set the value and actually be able to use the

77
00:05:38,170 --> 00:05:38,800
information.

78
00:05:38,800 --> 00:05:49,290
So go ahead and we're going to create 4-F base type var ref base wups of type database reference and

79
00:05:49,290 --> 00:05:52,940
then inside of this we're basically just going to return ref base.

80
00:05:53,250 --> 00:05:54,130
OK.

81
00:05:54,900 --> 00:06:03,840
Next we're going to go ahead and create one for ref users of type database reference.

82
00:06:03,840 --> 00:06:06,000
And we're going to return ref.

83
00:06:06,030 --> 00:06:12,320
Users can keep doing this for the others for groups and feed ref groups.

84
00:06:12,330 --> 00:06:18,930
Whoopsies of type database reference return ref groups.

85
00:06:19,050 --> 00:06:26,380
And finally var ref feed of type database reference return ref feed.

86
00:06:26,880 --> 00:06:31,280
All right so now we have four public variables that are accessing the private ones.

87
00:06:31,290 --> 00:06:38,670
So now what's going to happen is if we ever need to set a value or access a value we can access ref

88
00:06:38,670 --> 00:06:39,190
groups.

89
00:06:39,210 --> 00:06:44,100
And what it's going to do is it's going to give us the value of these private variables so we can access

90
00:06:44,100 --> 00:06:50,970
the information of these by pulling from a private variable that way these themselves if they're modified

91
00:06:50,970 --> 00:06:55,050
it's going to only return the value that we've set which is pretty cool.

92
00:06:55,050 --> 00:06:59,790
So next what we're going to do is we're going to create a little function that's going to allow us to

93
00:06:59,820 --> 00:07:07,900
push a user into the user's feed and basically it's going to add a user into the Users folder.

94
00:07:07,900 --> 00:07:11,490
And to explain this more I want to show you on firebase how this works.

95
00:07:11,490 --> 00:07:17,850
So here is our database and of course you can see there's no values there's nothing in here but let's

96
00:07:17,850 --> 00:07:20,750
say that I wanted to add a user's folder.

97
00:07:21,200 --> 00:07:28,650
OK I can create a user and what I can do inside of this users directory is I can create a new user so

98
00:07:28,650 --> 00:07:34,680
maybe I create a user with an ID of that Kate has a unique ID.

99
00:07:35,070 --> 00:07:41,010
And inside this user is going to have an email and you know what let's make it a string and email and

100
00:07:41,010 --> 00:07:42,580
their email is Bob.

101
00:07:42,600 --> 00:07:43,940
Bob dotcom.

102
00:07:44,200 --> 00:07:44,920
OK.

103
00:07:45,390 --> 00:07:51,900
And what else is our user going to have our users going to have an account type and their account type

104
00:07:51,900 --> 00:07:53,200
is e-mail.

105
00:07:53,310 --> 00:07:58,680
And when I click and watch what happens now there is a user's folder.

106
00:07:58,680 --> 00:08:04,710
If I open it up there is my user's I.D. and if I open that up you can see the e-mail and the account

107
00:08:04,710 --> 00:08:05,880
type values.

108
00:08:05,910 --> 00:08:10,470
So that's sort of how firebase works we're going to create a function that pushes that information to

109
00:08:10,470 --> 00:08:11,300
firebase.

110
00:08:11,310 --> 00:08:13,290
So let's go ahead and delete that.

111
00:08:13,290 --> 00:08:17,910
And just like that it's gone head back into X code.

112
00:08:17,910 --> 00:08:18,650
And here we go.

113
00:08:18,660 --> 00:08:19,810
Let's create that function.

114
00:08:19,860 --> 00:08:23,250
And you know what I'm going to actually give us some space here so we can see.

115
00:08:23,520 --> 00:08:32,190
And beneath this we're going to go ahead and call phunk create D-B user and we need to think in order

116
00:08:32,190 --> 00:08:33,150
to create a user.

117
00:08:33,150 --> 00:08:39,060
I just showed you I used a unique key and in this app we're going to actually call it the you I.D. the

118
00:08:39,060 --> 00:08:40,980
unique identifier.

119
00:08:40,980 --> 00:08:46,290
So let's go ahead and in order to create a database user We're going to pass it a unique ID of type

120
00:08:46,290 --> 00:08:51,480
string and we're also going to go ahead and pass it user data.

121
00:08:51,600 --> 00:08:58,990
Of course it needs the account type it needs of course it needs the account type it needs to email all

122
00:08:58,990 --> 00:08:59,830
that stuff.

123
00:08:59,890 --> 00:09:05,830
And the way we're going to pass that in is by giving it a dictionary wups a dictionary of type string

124
00:09:06,340 --> 00:09:12,420
and any Ok so that means we can pass it a string and any value can be on the other side.

125
00:09:12,760 --> 00:09:17,560
Now in order to actually push values up to firebase what we're going to do is we're first going to call

126
00:09:17,560 --> 00:09:25,960
ref users because we want to access the user reference which is at our default database slash users.

127
00:09:25,960 --> 00:09:29,670
And once we are in there we're going to create a new child.

128
00:09:30,100 --> 00:09:30,770
OK.

129
00:09:31,150 --> 00:09:35,090
Now the path string we want to give the ID right.

130
00:09:35,140 --> 00:09:41,440
We want the folder inside of users to be named the unique identifier for the user so let's pass in wups

131
00:09:42,070 --> 00:09:44,980
let's pass in the ID as the path string.

132
00:09:45,130 --> 00:09:48,930
And then inside of that we want to show the dictionary of user data.

133
00:09:48,940 --> 00:09:52,750
So go ahead and call update child values.

134
00:09:52,750 --> 00:09:58,630
That means that inside this unique child we're going to pass in the user data dictionary and you probably

135
00:09:58,630 --> 00:10:00,900
noticed that it's asking for a dictionary.

136
00:10:01,060 --> 00:10:05,160
Let's go ahead and pass that in user data.

137
00:10:05,230 --> 00:10:10,330
So believe it or not this function can be used to create a firebase user.

138
00:10:10,420 --> 00:10:14,680
And in the next video what we're going to do is we're going to set up our authentication view controller

139
00:10:14,910 --> 00:10:19,390
our log in view controller and we're going to start pushing users up to firebase and authenticating

140
00:10:19,390 --> 00:10:20,650
them with their email.

141
00:10:20,650 --> 00:10:21,780
Super super cool.

142
00:10:21,880 --> 00:10:22,590
Awesome job.

143
00:10:22,600 --> 00:10:24,220
And we'll see in the next video.
