Hey buddy this is Caleb with slopes and in this video we're going to build our data service it's a singleton
class that we're going to use to interface with our firebase databased.
Very very cool.
So let's go ahead and pull open our code project and what we're going to do to begin is to create a
new Swift file in our services group.
So right click on services click new file and create a new Swift file.
Now of course we're going to name this data service and make sure that you are adding it to the proper
target and click Create now inside the data service we need to create a class just like you might expect
by typing class data service.
Then to make it a singleton class meaning to make it accessible to any other class throughout the life's
lifecycle of this application go ahead and create a static variable static Erse that it constant actually
static let and then call it instance.
And we're going to just instantiate data service so basically we're creating an instance of this view
control of this class inside itself.
Very cool.
Not too bad.
Now what we need to do is we need to set up a few different things.
But before we do that we need to import firebase.
So go ahead and do that.
And now we can actually communicate with firebase now that we've imported it which is very cool.
OK.
So now what we're going to do is we're going to go ahead and create a constant outside of our class
called D-B base.
Go ahead and type let DBI bass in all caps and we're going to set this to basically allow us to access
our database like the base you Arel of our database.
To do that go ahead and type database database.
That means the primary database and then dot reference.
OK.
And if you look this is a function that gets a F-I our database reference for the root of your firebase
database.
Now if I take you over here to firebase I went into our project I went into the database and you'll
notice right here there's a u r l.
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
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
server.
So this is instantiated outside of our class so that it's accessible to us in the class because this
class by itself doesn't have any initialiser So we're initializing it outside of the class on purpose.
So now we're going to go ahead and make for private variables.
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
group or in firebase we call them a child.
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
thing like nested folders kind of I'll show you later.
But for now we need to know we're going to create a variable for our base you Aurel the your for the
users that we're going to store because if you look at our simulator here we need to basically be able
to hold information for all the users all the emails and all of the account types for the groups we
need to hold all of the group information as well as all the messages in each group.
We also need to be able to hold all of the information from the feed.
So we're going to make for different variables and we're going to use those to store those references
to the firebase database.
So let's go ahead and do that and we're going to use data hiding as well.
So that basically we can only set these values from inside the data service so that nobody else can
access them no other class or no other method can access these except for the data service itself.
Good way to be a safe coder.
So go ahead and type private var setting it to be a private variable.
And we're going to use underscore ref base that's going to be equal to D-B base.
OK so that's our database and we're going to create an accessor to set the value of this in just a second.
But for now we're going to just leave it as D-B base.
Now we're going to do another one here private var ref users and this is going to be used to save our
users into their own sort of folder in firebase.
Now to do that we're going to basically just append a child to D-B base like this D.B base child and
the path string is going to be users.
OK.
Now the cool thing about firebase is if a value does not exist when you go to try to save something
to a certain child if it doesn't already exist it will create it thus preventing crashes.
It's pretty amazing.
So I'll show you how that works as well later but for now just think if this is like we're creating
a folder to hold all the users can let's do it again a private var ref groups equals D-B base dot child
and the path string is.
Groups just like users and then go ahead and type private var ref feed trips to all caps equals DB base
child and the path string is feed.
OK.
Very very cool.
So these are private variables.
Now we're going to create public variables in order to set the value and actually be able to use the
information.
So go ahead and we're going to create one 4-F base type var ref base whip's of type database reference
and then inside of this we're basically just going to return ref base.
OK.
Next we're going to go ahead and create one for ref users of type database reference.
And we're going to return ref.
Users can't keep doing this for the others for groups and feed ref groups.
Whoopsies of type database reference return ref groups.
And finally var refeed of type database reference return Ref field.
All right so now we have four public variables that are accessing the private ones.
So now what's going to happen is if we ever need to set a value or access a value we can access ref
groups.
And what it's going to do is it's going to give us the value of these private variables so we can access
the information of these by pulling from a private variable that way these themselves if they're modified
it's going to only return the value that we've set which is pretty cool.
So next what we're going to do is we're going to create a little function that's going to allow us to
push a user into the user's feed and basically it's going to add a user into the Users folder and to
explain this more I want to show you on firebase how this works.
So here is our database.
And of course you can see there's no value there's nothing in here but let's say that I wanted to add
a user's folder.
OK I can create a user and what I can do inside of this users directory is I can create a new user so
maybe I create a user with an ID of that Kate has a unique ID.
And inside this user is going to have an email and you know what let's make it a string and email and
their e-mail is Bob.
Bob dotcom.
OK.
And what else is our user going to have our users going to have an account type and their account type
is e-mail.
And when I click and watch what happens.
Now there is a user's folder.
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
type values.
So that's sort of how firebase works we're going to create a function that pushes that information to
firebase.
So let's go ahead and delete that.
And just like that it's gone head back into X code.
And here we go.
Let's create that function.
And you know what I'm going to actually give us some space here so we can see.
And beneath this we're going to go ahead and call phunk create D-B user and we need to think in order
to create a user.
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
unique identifier.
So let's go ahead and in order to create a database user We're going to pass it a unique ID of type
string and we're also going to go ahead and pass it user data.
Of course it needs the account type it needs of course it needs the account type it needs to email all
that stuff.
And the way we're going to pass that in is by giving it a dictionary wups a dictionary of type string
and any K so that means we can pass it a string and any value can be on the other side.
Now in order to actually push values up to firebase what we're going to do is we're first going to call
ref users because we want to access the user reference which is at our default database slash users.
And once we are in there we're going to create a new child.
OK.
Now the path string we want to give the ID right.
We want the folder inside of users to be named the unique identifier for the user so let's pass in wups
let's pass in the ID as the path string.
And then inside of that we want to show the dictionary of user data.
So go ahead and call update child values.
That means that inside this unique child we're going to pass in the user data dictionary and you probably
noticed that it's asking for a dictionary.
Let's go ahead and pass that in user data.
So believe it or not this function can be used to create a firebase user and then the next video what
we're going to do is we're going to set up our authentication view controller our log in view controller
and we're going to start pushing users up to firebase and authenticating them with their email.
Super super cool.
Awesome job.
And we'll see in the next video.