[MUSIC PLAYING]
DAVID EAST: What's up, everybody?
David here from the Firebase team.
And welcome to a new season of Firecasts.
Today, I'm going to teach you everything
you need to know to get started with iOS and the Realtime
Database.
I'm going to show you how to get set up
and then teach you the basics by building an app.
So let's dive in.
I'll be using CocoaPods to install the Realtime Database
SDK.
And if you want to learn more about CocoaPods,
you should totally watch Todd Kerpelman's video,
which will teach you plenty about getting
started with CocoaPods.
So I'm going to create a pod file by calling pod init.
And this creates the manifest of all of the dependencies
that my iOS project will rely on.
So to specify these dependencies,
I'm going to open up the file in VIM.
So CocoaPods allows you to specify specific targets
in your project.
So this is our main target right here.
And then inside of this do block,
we're going to specify the Firebase dependency.
So to specify the Firebase dependency,
I'm going to write pod Firebase.
And this is the core of Firebase.
But I also want to make sure I get in the Realtime Database.
So I'm going to write pod Firebase/Database.
And that's pod Firebase slash Database.
Now, I'll save and quit and then do a pod install.
Once the install is complete, I'm
going to open up Xcode using the newly created XE workspace
command.
So open for quickstart.xe workspace.
And that's all it takes.
Now that we're set up, let's build that app.
We're going to build a social, local, mobile, crowdsourced,
weather app that'll tell you whether it's sunny or foggy
in San Francisco.
It's a pretty basic app, but it covers
the fundamentals of saving and synchronizing
data in real time.
And if you build across multiple platforms,
we have the same video for Android.
So let me show you how this app works.
So now that the set up is complete,
I need to configure Firebase for the app.
And I'll do that in the app delegate class
by creating a custom initializer.
So open up App Delegates.
And then I'm going to create an override init.
And then inside of here, I'm going to configure Firebase,
because when the app launches, it will call this.
So I need to import Firebase.
And then inside do fir app.Configure,
which sets up everything you need to get
started with Firebase in iOS.
So now let's build and make some room for the simulator.
And there's nothing here.
But that's awesome because we didn't hit any errors.
So now we'll open up the Storyboard
and start to build out the UI.
So I'm going to make some space right here.
And then I'm going to drop in a label
to represent my weather condition and then two
buttons-- one for sunny, and then I'm also
going to create one for foggy.
And I will use some automatic auto layouts.
And then when I build, we have our app.
So now I'm going to open up the View Controller
and create a reference to the real time database.
And since we're using Swift, I'm going
to go import Firebase at the top.
And then now to connect that to Firebase,
I need to create a reference.
And the first thing I'm going to want to do
is connect out to the database.
And to do that I need to create a reference.
You want to call this reference rootRef.
And what this reference will be set to
is F-I-R database.database.reference.
And this creates a connection to the real time database
and gives you access to sync and store data at a given location.
And the location given to us at this point
is just the root of the database.
Because the real time database is just
one big hierarchical JSON tree.
So now I want to synchronize data whenever
the weather condition changes.
So to do that, I'm going to create a child reference
at the condition path.
So I'm going to call rootRef.child and pass
in the string of condition.
And essentially what this does right here is that it starts us
out on the root-- on rootRef right here--
but then calling dot child will nest us
in to the condition key.
And this condition is where we're
going to store sunny or foggy.
So now that I have access to the condition location,
I'm going to listen in real time for whenever it updates.
And to do that, I'm going to call observe event type.
I'm going to pass in the value event and then a closure that
returns a data snapshot.
So this is pretty long right here.
So we actually can go and break out the rootRef.child
into its own variable.
Because when you call dot child, it just
creates another reference.
So let's copy this.
And then we'll create a child reference called condition ref.
And then we just replace it right here.
So I need to synchronize the condition label with the latest
value.
So I need to create an outlet for the label and actions
for the button.
I want to synchronize the label with the latest value.
So I'll need to create an outlet for the label and actions
for the buttons.
So go back to the Storyboard.
And then I'll go up to the Dual Mode,
and I'll drag in the outlets called
condition, Label, and then some actions for the buttons.
Sunny [? did ?] touch, and then also one for foggy.
And that will be-- drag that here.
And then foggy [? did ?] touch.
So now I can go in open up just the View Controller.
And then inside of this observe event type block,
I want to synchronize to the condition label.
So I'll grab the self.condition label, and grab its text,
and set that to the snap.value.description.
So now let's run the app and pull up the real time database
viewer.
So we're running the app right here.
And then we're also in the real time database viewer.
And so we don't have any data right now.
It's just null.
So I'm going to go and add a condition path.
It's going to hover over here.
Hit Plus.
Condition is the key.
And the value, I'll just say sunny.
When I click Add, it adds the data.
But you can see that nothing happens.
And that's because of the default security permissions
on the real time database.
So I'm going to click on the Rules tab.
And then what you can see inside here
is that we have our security rules.
We have rules for dot read and write.
And what this means is, is that any authenticated users can
write to anywhere in the data.
But currently, we don't have any authentication enabled,
so no one is going to be able to write data.
So what I'm going to do, since I'm in development,
is I'm going to change these rules to allow anyone
to read and write data to the real time database.
And this is fine for development,
but you should never, never, never go
to production like this because you will have no security.
So you should definitely write rules for your data structure.
So now I've refreshed the app.
And you can see right here that we have the value sunny, which
is our condition right here.
And to prove to you that it works,
I'm actually going to go and edit it.
So I'm going to change sunny to foggy.
And then just like that, it updates in our application.
So the last step is to change the label for when
the buttons are tapped.
So if I tap Sunny or Foggy, what I want that to do
is update the real time database, so slash condition.
And therefore, because this updated,
it's going to trigger that real time event
and update the label.
So back in Xcode I'm going to take this condition reference
and break it out to a property.
So I'm going to copy it right here,
and then I'm going to paste it below root reference.
But Xcode isn't going to like that.
And that's because you can't use this property yet.
So what I can do is, is I can take this line
right here that gives us a reference, and then just
call dot child.
So now in sunny [? did ?] touch, I'm
going to call a method called set value
and give it the value of sunny.
And what you should notice here is that we're
using the condition reference.
And we're setting its value to this specific value.
So if you remember, the condition reference
is a child from the root called condition.
And this, when we call set value,
will give it a value of sunny.
And what will happen is, is we'll
update the condition, which will therefore fire off
the real time value listener, which will update our condition
label.
So it's really important to notice
the dataflow here, is that we're not modifying
the label directly or some local data source,
is that we're calling set value and we're
letting the real time database synchronize the changes.
And so now all I have to do is copy this for foggy,
and then change the value to foggy.
And just like that, we've built a real time app.
And there you go.
You're all set up with a real time database
and only in a couple of minutes.
And that's all for this Firecast,
but please leave your questions in the comments below.
Or reach out to us on Twitter or G+ with the hashtag
#askFirebase.
And we'll be dropping a new Firecast every few days.
So don't forget hit the Subscribe button
to stay up to date.
I'm David East, and thanks for watching.
[MUSIC PLAYING]