It's time to start implementing Firebase in Fury, and we're going to do all of this inside of The View
controller.
Now, if you are making a production app, a commercial app of some sort, then you probably want to
abstract the firebase stuff away into its own class and then listen to that class from the particular
view controller you're looking at for two reasons.
One, so it's maintainable, you know what you're changing and where.
And two, you don't confuse your view controller code or have to repeat your code when you create another
view controller that displays the data slightly differently.
You just want to access one class for your data.
OK, so we are not going to reinvent the wheel.
We're going to look at the way that I've done this before.
So we need, first of all, to declare three objects.
The first object is a variable for messages and we're going to get a data snapshot array from Firebase.
That's a snapshot of the data.
At a particular point in time, we are going to get a database reference from Firebase.
That's pretty self-explanatory.
And we're going to get a database handle on the database objects so we can go and access that database
whenever we need to.
OK, and that's giving us an error because we need to import these two items because we only have Firebase
Auth in here right now.
OK, so we've got our database ready to go now.
We need to go and set that up.
So the first thing that we're going to do is create a method called a set up firebase.
So I'm just going to copy this straight over.
Just below did load and then I'm going to call that down here now, in the end, this might have an
effect on our UI because we're checking the user and setting up Firebase.
We might want to set up Firebase only if the current user is logged in.
So that's a problem we can fix later on.
But for now, our app is set up, so our user is always logged in.
OK, so we can worry about that little bug later on.
And I only know that's a bug because when I was preparing the update for this course, I discovered
my own bug.
Right.
So in our set up firebase and post this, if you need to copy the code, we have that reference which
we get from the FIREBASE database object.
And the current instance of that object is here.
And then we grab the reference of that.
Then we have a reference handle on a particular part of that database.
So we need the ref for that.
And we tell it, I want you to go and look at the child called messages.
So in the firebase database.
And I don't think I actually have any here.
Not yet anyway.
We are going to have a sub child called messages now.
Important distinction if you've done SQL database before or any kind of databases.
Firebase is based on a document database like Mongo, for example.
In fact, I think it does use Mongar, but don't quote me on that.
That is different to how we structure other databases.
So don't think of a child as a table.
It's not a table of messages, it's just a subcategory.
The subcategories are very, very limited in the number of them you can implement in Firebase.
So don't arrange your data that way.
I'm doing that here.
So our app looks clear.
A lot of people would be tempted to go, you know, create a table for messages, create a table for
messages between these two people and these two people and these two people.
And you could see how that's going to expand crazily over time.
So you don't want to do it like that.
Instead, in the table of messages, you put everybody's message, you get your authentication rules
right so everyone can access everyone else's messages just their own.
And what's the final thing you have to do?
If you want to categorize the messages, you add an object into each message document so we know what
category that's in.
So that's just a warning for you from someone who's done it the wrong way before.
Then what do we do?
We're going to add an observer, so whenever this changes, Firebase will pay us a message and say it's
changed.
What kind of things are we going to listen to or we're only going to listen to?
Child added as in someone's added a new message to the stack.
Now, of course, you can customize this because you don't want everyone to be listening for a child
added when it's a private conversation between two people.
I'm just doing this quick and dirty for us so we can understand what's going on.
But you can, of course, customize that.
There are other ways.
Child added, child change, child moved, a child removed.
OK, so lots of things you can listen to.
Then if that event fires off, it's going to return a data snapshot from Firebase.
At that point, we're going to get that data snapshot and add it to our messages object.
Now, this is only going to give us the difference, it's not going to send over the whole data table,
if you like, it's just going to send us the difference.
The one that was added, if you have if you're listening to something like child changed, then you're
going to have to have a method that finds that one in your list of messages replaces it with the new
data.
OK, so that's just a warning.
But what we're doing here is just appending because it's the easiest way of doing things and showing
you and keeping you interested, then what we're going to do is go to the table view and we're going
to insert a row at the current index paff.
End.
OK, so this code gets the index path and it finds the end cell and we do the minus one because of the
zero based indexing there and it inserts that particular row.
OK, and then down here is just a nice little thing.
You don't need it, but it will automatically scroll the table up to the end of that row.
So when you get a message, just like in a message or WhatsApp, it scrolls to the bottom automatically.
OK, so that's just a nice little thing now.
So we've we've set up Firebase.
We've got all that reference.
Currently, there is no data in Firebase.
So what I'm going to do is, first of all, actually scrolling down the code, we're not going to see
anything away because the table view just has 100 items and the table view just returns a bog standard.
So we haven't changed it yet.
So let's return the number of rows in section.
Let's see what I put in the code here.
Just the messages don't count.
OK, and that will automatically update if we have more messages.
Now, I'm going to copy this code that's going to implement our cell faurot act.
So feel free to pause and copy this code.
Right, so first of all, we're going to grab the message, then we're going to get the message content
from the FIREBASE database as a dictionary of string and string.
That's just how it comes out.
Then we say, if you can get some text from those message fields, what message fields?
Well, we haven't actually set that up, so that's what we should do now.
We should have constant stop message fields set up.
And in my other code here, here we have it.
So I'm just going to copy this, paste it and explain.
All I'm doing is setting a file with some constants so we don't mess up what things are called.
So it's going to go, hey, if I want the date time, it knows to use the string.
If I want the text, it knows to use this string.
And these are the fields that we're going to use in Firebase to define the data that we're going to
pull through.
So once I've put that in, our silly error should go away here.
You can find it, oh, I know you can.
What if I press play it should find it then.
Yeah, there we go.
It's found it.
Yes, it has.
OK, so that's of doing, going and building itself and where we have our app again with an empty list,
because it's pulled nothing down from Firebase yet.
But if I insert some dummy data, then we're going to see some messages coming through.