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