We're finally ready to actually send our message, so I have this little stub, this little call to
send message ready to go, which gets the data.
So we've already written this, which gets the data on the text fields.
What I'm going to do is paste in the send message.
So pause and copy this if you need to, where we set up a packet, which is equal to the data and that
packet, which is just basically the data we're sending to Firebase, we're going to add in constants
date time.
OK, so we're giving our FIREBASE database a date time that this was created.
This is on the current device for this particular user.
If I was going to run this, if I was going to create this in a commercial sense, I would use the date
time of the server and just pick up the time zone of the user so I can add plus minus whatever it is,
depending on who's viewing that time.
OK, of course that depends if you actually want to do it that way or not.
So what I have to do is just copy in my get date code from utilities.
All I'm doing with the get date is formatting a date into a string.
So let's copy this then you can have a look at that.
This is just some standard code that I have written out in my my quick snippets.
So when I need the dates in iOS, this is what I go for and I know how to do it quickly, instantly.
In fact, with most apps, I just copy my utilities class because it's all of the things like show alert,
get date and clear TextField that you're likely to use all the time.
So utilities.
Well, I've got tons of those.
OK, so that's going to now insert the date time from utilities and then we're going to pick up this
child messages on the database side in Firebase and we're going to say, I want you to create another
child within messages, as in a data point that has an automatic ID.
And I want you to set the value of that point to as the data that I'm passing you here.
OK, so what I could actually do here is make all of this a bit more compact.
Is that going to work?
Oh, data is a let constant.
That's why it's up here somewhere.
Yeah, I said let see what we can do here instead of, say, far.
And now the error should go away.
I say it should go away.
Let's just try and build it and see what happens, shall we?
Something weird is happening here, so I'm going to revert all of this just to hit Command Z and now
I see why I had packett, it's funny, as a developer, you look through your code and you think that's
weird.
You change it and you go, Oh, yeah, I remember what I did there now.
So that's going to send the message.
Now, there is a very important thing that we've missed out here, Arava.
I've missed out.
Is the D in it OK?
Because and this is related to the pulling down of data when our view controller exits, we want to
remove the reference to that particular child in the database.
OK, I should have mentioned this when we actually set up the database because that's where it applies
to.
We're setting up an observer.
When we remove this view controller, we want to get rid of the observer.
OK, now back to sending things.
So here we have send message and send message is called in.
Text fields should return.
So I'm going to run this in our neighborhood simulator.
Let's see, what are we going to get?
So we have all the data that we should have from before we scroll to the bottom and I'm going to say
hi there, I'm going to hit enter, and that's the clue for it to populate in the database.
Now, if we go over to our firebase console, if we look at the very last section, we will see our
particular message.
And now if I'm fast enough, let's move this here.
If I type something random here and I'm going to press, enter and quickly change.
See that green that appears that indicates that that is a new addition to the real time database.
Now, this console that we're using is only really useful when the data is small like this.
When the data expands, it's pretty useless.
Let me tell you from experience.
But that aside, we have now plumbed in the ability to send messages from our app as well as receive
them and add them in to our table view.