Welcome back everyone.
Johnny be here with slopes dot com.
And this lesson we are going to finally make our first data entry into our fire store database are I.
The last lesson we got Are you all set up so that we can select a category and a user name and write
a random thought and then push this button to post it.
Right now it's not doing anything though.
So let's change that.
All right.
So we are going to first go to our fire base console or so open up your browser and head on over to
your dashboard for the project that we created a couple of lessons ago.
OK.
So you should be here on the overview.
And we're we're going to go we we're going to set up our database.
Right so come over here to database.
Now the previous version of the firebase database system is the real time database.
And as we covered in the previous lesson a couple lessons ago there are still times when you would want
to use a real time database but this lesson these lessons are all about using cloud firestorm.
So we're going to click here on try fyrst or Bayda and we have some security rules for cloud store so
we can start out in lockdown mode which makes your database private by denying all reads and writes.
Or we can start in test mode.
And this says get set up quickly by allowing all reads and writes to your database but you'll see here
a warning that says anyone with your database reference will be able to read or write to your database.
So this isn't particularly safe but just to get started.
And for this course we're going to go ahead and just do this in test mode so that everything is open
to read and write.
But again just to reiterate don't do this for production apps or this is just to get started and to
learn.
All right.
And future target topic.
We are going to actually go over using firebase authentication and how to log in and how to set up those
security rules in a better way.
All right.
All right so here we are in our fire store database.
Now if you remember from a couple of lessons ago when we were going over the fire store basics at the
root of our database we always have to have a collection.
All right.
And then inside of that collection we have our documents and our documents they can hold a number of
different types of data including strings booleans numbers dates gnoll et cetera and well actually see
that here in just a second.
All right and so we can actually manually create collections and documents right here from our dashboard
OK.
So if I click here we're going to say at collection and the collection name is going to be.
Thoughts.
OK.
And then we're going to need some documents.
Let's go ahead and create our first document here.
Now you we can specify a document I.D. we could say something like.
First thought
and that would be fine and then we could add our fields and everything or we could say auto ID and then
it will automatically generate just randomly generated document ID and then here we have the fields
OK.
So these documents they are organized in kind of like you could think of like dictionaries in Iowa or
Jason objects.
You have a document and then inside of it you have key value pairs.
However with fire store we call those keys instead we call them fields.
And then the values are still values.
So for our documents these are going to be our thoughts that we display.
And if we open up our preview for our mockup we kind of take a look at one of these thoughts that are
displayed.
We kind of get an idea of what we need to have stored in these documents.
We're going to need a user name.
We're going to need a time stamp.
We're going to need the thought text itself.
We're going to need to know how many likes each thought has and then we're also going to put in a field
for a number of comments and this will be for in the future when we implement comments on each of our
Random thoughts.
That's going to be useful then.
All right so let's go ahead and jump into here back into our dashboard forecourts for firebase and let's
manually create our first thought.
OK.
So here we have the field and we're going to call this category and it's going to be of type string.
And for the value I'm just going to initialize it to be funny.
OK.
And then we can add another field and this one we're going to say and some comments for number of comments
and this type is going to be of number and we'll initialize it to zero.
I'll say add a field and then this one is going to be the number of likes that The Post has that's going
to be number likes.
And that's also going to be a number and we'll set that to zero and then we're going to create.
So this one is going to be the text for the thought.
So it's a thought text thought T T S T and we're going to write.
This is a random thought and it is of type string.
And then we're going to need a time stamp.
And this one is of Ty time stamp and you can see here all of the different types that you can assign
for a field if string number Boolean object array Noall timestamp G.O. point and reference.
OK.
So those are all some very useful things that you could add came the time stamp was go ahead and just
set a date.
Can we just leave that as zeros then we're going to have our final field which is user name.
And then all right Johnny B.
And we can save this.
And now we have our very first let's see what's going on.
I'm going just going to refresh this.
There we go.
That's better.
All right so now we have our thought document.
And so you see here that it follows the pattern collection collection his thoughts and inside of that
thoughts collection we have a number of documents and then we have the document.
And here are its fields and then if you wanted you could have another subcollection to this document.
And inside of that subcollection you would have other documents and.
And so on and so forth.
So always following the pattern collection document collection document.
So in a future target topic when we implement comments for each of these thoughts then each of these
documents is going to have a subcollection of comments and each of those comments will be a document
inside of that subcollection.
All right.
So some pretty cool stuff.
All right so yeah we have rent.
We have manually placed our very first thought in our database.
That's pretty cool but not nearly as cool as actually doing it from our app.
All right.
So let's go ahead and make it so that we can do that from the app.
All right so we're going to jump back into X code and this is where we are going to do it right here
in the post button tapped.
All right so let's do a little bit of prep work.
First we're going to need to be able to have a way of setting the category and getting the thought text
and the user name.
All right.
So let's create a variable here.
This variable is going to be called when I make a little header for variables but I'm going to spell
it correctly.
The variables that we go and when you create a variable here are going to save private var.
I'm going to call it selected category.
OK.
And this is going to keep track of which category is selected when we go ahead and send them to get
my simulated back up here.
When we click on the and we select the different types of thoughts that we can post.
All right.
And I'm going to just initialize this too funny to start out with.
That looks good.
And then we were going to need to import firebase houmous to import firebase and there we go.
So it doesn't matter which SDK you're working with.
All you need to do is import firebase and that will cover the whole umbrella of different STK that you
could be working with.
All right so now we're here when we post on the button tapped how it works when we are saving data to
our online database where we need to do is we need to get a reference to our collection or our document
that we want to write to.
And there are a couple of ways of doing that we could say fire store that fire store.
OK.
And then we need a reference to our base collection.
OK.
And so we can say collection path right here.
And this is this gets a fire collection reference referring to the collection at the specified path
within the database.
OK.
And our collection is called thoughts.
OK.
So this right here this gets us a reference directly to this collection here in our fire store database.
OK.
And then once we have that what we can do is we can say Dot and document and so here we can see we have
had document and add document with a completion handler.
OK so if we did this one then we wouldn't have any way of knowing once it was complete.
But we want to know when it's complete so that we can segue back to our initial controller as well as
Hendel if there are any errors.
Ok so I'm going to select the on the completion handler we go and then I'm just going to press return
again and then I'm going to tab and press return again to get the completion handler then to rename
this error to just air e r r.
OK.
And then just the area of this area quick I'm just going to put empty strings are right here.
Looks good.
And then right here in the code this is where we can handle the error.
So we're going to say if the air is equal to air so if there is an error and then we're going to sit
equal to air and we're just going to debug printers to say Debug.
Print and I'm going to say error adding document
and then we're going to print out the error and say error.
OK.
Else if we were successful then I'm going to say self that navigation controller and we're just going
to pop this view controller and we're going to stay true.
So when you're using a navigation control you can use this method poppier you controller to just go
backwards to the previous controller in the stack.
OK.
All right so now it's come back up here to our data.
All right so we are saying add a document and the document that we are adding is this data OK.
So we're going to do here is we are going to construct a dictionary that contains all of the data that
we want to send up to our database.
So we're going to have a dictionary of key and values that contain the information that we want to send
up.
So for instance we have up here and this is a data dictionary.
OK.
So we're going to have key value pairs for each one.
OK.
So we're in the category.
So here I'm going to write the category and the string that we're going to be setting up.
This is the selected category here.
So when I say selected
category
get and then just to make this a little bit easier to read I'm going to write after this quick break
here I'm going to do a return and then right here after I select the category I'm going to do another
return tape and then after select category I'm going to do a comma because we have a number of things
that we're going to add here.
All right and so the next key value pair is numb comments.
OK so this is going to be the field name that we send up in our document and it's value is going to
initialize that to zero.
Then when her next fuel the value pair is numb comments and its value is also going to be initialized
to zero and then we need the thought text.
OK.
So let's see.
This is going to be thought text and it is.
So what did we call our label here thought text.
All right.
So we're just going to say thought text dupped text.
And then the next one is a time stamp.
OK.
And for this one how we can initialize a time stamp is we say field
value dot server time stamp and this is used with said data or update data to include a server generated
time stamp in the written data OK.
So we're just using this method to create the time stamp at the exact moment that we create this using
the server time stamp.
OK.
And then lastly we need the user name.
OK.
So user name and the value is and this is going to be the user name.
Text that text and that for now will just force unwrap this with a bang.
All righty.
And that's it.
We have created our function that is going to send up all of the all of this data to fire store and
create a brand new documentary.
So just to recap what we did is we got a reference to our collection by saying fire store fire store
data collection and we pass in the name of the collection here.
And then we see ad document.
So we're going to be adding a document to our collection of thoughts and then we set the data that is
going to construct the document that we are sending up.
And this is a dictionary of key value pairs.
And so we set all of the keys and all of the values and then we have a completion handler where we can
handle the error if there is one and we just debug printing it for now.
And if it's successful then we are going to pop back to our main visi.
All right let's go ahead and test this.
I want to run it right so I have the app here and then I'm going to open a bar Safari window as well
so that we can see if this automatically pops up here.
So I'm going to go to our plus button here I'm going to click on it and I got also a collection of kind
of just some silly random thoughts that I got from the internet.
I want to copy that here and I'm going to paste it in here.
Let's see here.
OK.
And then the user name is soil work fanatic.
All right so these are from Reddit by the way.
And then I'm going to say post and if all goes well we should see a newly generated Auto-ID pop appear
with all of the information that we set.
All right.
So when I say post oh we got an error.
All right let's see what's going on here.
You'll see here that we have a fatal error dictionary literal contains duplicate keys.
That's because I accidentally wrote comments twice in a row.
The second one should be numb this.
Never go so let's try that again.
All right then let's get our database back open and are happy and let's try that one more time.
OK.
So here I'm going to pace to that random thought back in the paste and the user name was Soilwork phonetic.
OK and let's try that one more time post umu Hey and we dismiss back to our initial Visi And here in
our collection we have a brand new document.
If we click on it sure enough we can see the category the number of comments number of likes the thought
text and the time stamp and the user name.
All right so that is pretty cool.
Let's let's let's do another one.
Let's come back to our ad post.
Make sure this is still working and that someone can do a post by Jack Handy.
He was a character on SNL for a while.
Super funny guy and he had a collection of funny sayings called simple thoughts.
And so I'm going to paste one of those here.
All right.
And so let's post that again and check out over here make sure it pops up when we get it.
So we've got a new one and see here.
Yep there we go.
So we've got the category number of likes.
Everything.
All right.
Very nice.
So you can see how simple firestorm makes it to post documents to our database.
All right.
One thing that I want to touch on here is that we get a reference to our collection called thoughts.
But if this this collection didn't even exist then none of these documents existed.
We.
And so this was the very first time we were making a post.
We don't have to manually create a collection called thoughts.
If we say fire store dust collection thoughts and we had a document that will implicitly create the
collection called thoughts and the document.
All right.
So you don't have to manually create collections or documents and then add to them you just tell firebase
that we want to create them and then if they don't exist they'll be created.
If the collection exists then the document will be created inside of the collection.
Right.
All right so that's cool.
All right so one last thing that I want to do to kind of clean this up a little bit.
Right now we don't really have a way of setting the category that is selected.
We have our action for when we go ahead and select one of the different categories but we're not saving
that anywhere aren't.
So I'm thinking is first let's create an enum an enumerator to store the different types of categories
that we can select from.
So let's do that here in our main Visi up here outside of the class declaration.
Let's create an enumerate just means that num.
And we're going to call that thought category and of type string.
And our cases are going to be case serious and then string serious case funny and then string funny
and then case crazy and then string crazy and lastly case popular because it's going to be one of the
ways that we can sort the data and that is going to be popular.
And there we go.
And then the show back into our ad thought Visi and all we can do here is we're going to still initialize
to funny.
But instead of having just the string funny we're going to say thought category not funny that raw.
OK so the real value gets the actual streaming value for the case.
And then down here in our category change so each time that we click on one of those different segmented
controllers for funny serious crazy we're going to set the value for our selected category to the correct
one.
So we're going to do a switch statement.
We're going to see switch and the value that we're switching on is the category segment not select that
segment index.
Okay so I'm going to say category segment dot selected segment index.
Okay so we're doing a switch statement for each time that we change the selection in our segment control.
Okay.
And so for the first case we're going to zero which is the funny cat and we're going to just say selected
category is equal to thought category diet.
Let's see here.
Funny that raw value.
And then for a case of one.
So this is the second segment to control that's going to be you select the categories equal to thought
category and serious not a raw value.
And then we'll just set our default value to be the third segment and that is going to be selected categories
equal to 4 category that crazy to raw value.
So now each time that we click on one of those Let's get our simulator here.
So now each time we click on one of these we are resetting the variable for the for the selected category
to be a string of value for funny serious or crazy.
All right that's good.
All right so let's get rid of this being operator right here let's come up here and we're going to do
a guard statement here we're going to say guard let user and name equal to user name text dot text else
and we'll just return.
OK.
And then down here instead of using in text text we're going to say user name.
Now one last thing that I'm not liking is we have all of these magic strings just lying around.
What if I had accidentally mistyped one of these and we're going to be using these in other places.
Well so let's go ahead and make these constants I think.
So when we create a new group here a new group run call these utilities can it create a new file and
treat us with file here and we're going to call this Constance or I and this is we're going to store
all of our constants say create and we're going to have a constant for our thoughts collection case
when we say let thoughts underscore ref.
And it's common practice to use capitalized letters four to denote constants so say thought to underscore
equal to thoughts and then let category equal to category and then let some comments equal to some comments
and then let them likes equal to likes and then let thought text equal to thought text and then let
time stamp equal to time stamp.
And lastly let user name equal user name.
So now we have a cup Constance for each of these field values and our thoughts reference.
I'm going to save that and jump back into the sea and let's just go ahead and replace those categories.
Now get to Ghori.
Not sure why in my article it isn't working.
But Num. comments is now numb underscore comments like likes is now underscoring the likes thought text
is now thought underscore text the time stamp is now time stamp and user name is now a user name.
All right.
That looks good.
Let me make sure.
What do we have here for the thought text Constans.
Oh that's because I spelled it wrong right here.
OK.
So you g h t just going to copy that.
Come back to our I had thought.
And replace that.
All right so that's looking good.
It's going to run this one more time up and we can we can replace this right here as well.
So thoughts.
Ref.
There you go my autocomplete is back now.
All right so let's go in and run this and make sure everything is still working as expected.
All right so here we are.
Let's add another random thought and just can put Johnny B.
And I'm just going to write some random gibberish and say post.
And here we go.
Let's see here category and I'm calling some likes.
All right everything is looking good.
All right so congratulations you are now working with firestorm from your app adding data to your database.
In the next lessons we are going to be covering some really cool stuff on how to retrieve that information
how to filter it how to sort it how to add listeners for real time updates.
And yes it's going to be really cool.
First up though in the next lesson we're going to do a little bit more work so that we can set up our
table view and have a place to be able to display that data that we're going to be working on retrieving.
All right so I'll see you in the next one is going to be great and I'll see you then.