Everybody this is Caleb with slopes dot com.
And in this video we're going to create our group model there.
We're going to use that to download all of our groups from firebase and pass it to groups Visi where
we're then going to unload it and pass it into the table view so we can see every group that we're in
created and have been invited to.
So let's pull open our X code project and we are going to start by going to our model group and we're
going to right click and create a new file.
Now of course this will be a swift file and it's going to be called group.
Now we need to think about what kinds of things go in a group what do we need.
Well when we add one we add title a description and we give it an array of members.
Now something else a group should have is a unique key right which it does in firebase but we should
add that to our model layer.
And we should also keep track of how many members there are in our group.
So very cool.
Let's go ahead and let's create this class so-call class group and now we're going to create five private
variables to hold all of those properties that we need.
So private var underscore and we're going to go with group title of type string.
So a group needs a title private var group description of type string.
A group also needs a key or a unique ID of type string.
Once a group is going to need a member count of type in pay attention to that we're also going to need
private var members and that's going to be an array of type string.
OK and that's going to contain all of the names of our members.
And we're going to use that when we actually open each group and it'll pass in the member names and
we'll show them on the View Controller for each group.
So we now have five variables and now we're going to go ahead and set up some public variables that
we're going to return the values of these two.
So go ahead and create one for group title var group title of type string.
And we're just going to return group title next group description of type String.
We're going to return group description.
OK keep doing this for key and the rest.
What's key is going to return key
and then we have member count member count of type int.
We're going to return int member count member count then we'll have you know what.
It's hard to see let's go ahead and put this up a little higher.
Var members is going to be an array of string.
And we're going to return members members.
There we go.
So now like any model there we need to initialize everything for when we create it.
So let's go ahead and call in it and we're going to pass in parameters.
So we're going to give a parameter for the title of type string.
The description description of type string.
The key of type string the members which is an array.
Sorry.
Members an array of type string.
And then the member count of type int.
Now to initialize we just call a self underscore group title.
And we're going to set that to title of course self dot group description.
It's going to be description self-taught key underscore key sorry.
It's going to be equal to the key we pass in from our function self-taught members underscore members.
Sorry is going to be equal to the members we pass in from our function and self-taught member count
with an underscore is going to be equal to member member count.
There we go.
So when we initialize this we're going to initialize all the values and that's exactly what we need
to do to create a group.
We'll use this model to pass a group into the view controller when we select one.
So for instance in our simulator here let me pull it up and open our app when we go into groups and
if we select a group it's going to need to pass in this Daim the members the member count the key.
All that information so it can display all the relevant messages and users on the next screen.
That's why we're creating a model there.
So now that we have a model where we can go ahead and write our function in our data service that's
going to pull all the groups down from firebase and create an array of group thanks to our model there.
So let's do that go ahead and type phunk get all groups.
OK.
And this function of course is going to need to return or I guess escape values from the closure.
So we're going to use a handler like we have throughout a lot of this course.
It's going to need to be a scaping so we can get the values out and we're going to go ahead and return
nothing.
But we are going to pass a value to our handler and we're just going to call this group's array which
is going to be an array of group that's our class that we just made.
Now we're going to go ahead and just like above we're going to create an array.
We're going to create a for loop to cycle through all the groups and we're going to create a handler
that's or we're going to use our handler to pass that array back once it's full.
So go ahead and just call var group Saray and it's going to be an array of type group as you might have
expected.
Now we're going to go ahead and look at our group's reference so call ref groups and we're going to
observe a single event kind of type value.
So we're just going to pull all the values down.
And of course we're going to have a data snapshot which we can call group snapshot.
So what we're going to do is we're going to create a constant that's going to hold the value of all
the objects of this snapshot.
Then we're going to for loop through them.
So just like before we'll type God let groups snapshot equals group snapshot children.
Whoops sorry no s there.
Group snapshots.
Children.
All objects.
And of course we're going to cast it as an array of data snapshot.
Else there's nothing we're just going to return.
Very cool.
So now that we have our group snapshot Let's go ahead and cycle through all the groups by typing for
group in group snapshot.
And as we do that this little arrow will go away saying that we've never used it because we did for
group in-groups snapshot.
We're going to basically need to pull down the array of members.
Now we need to look at firebase to see what I'm talking about here.
But when I created a group earlier we passed it an array of members.
Now let's go in and look at it.
So right here members now this is an array.
So we need to pull it down as an array and then we basically need to pull the information from it.
OK.
So let's go ahead and let's set up that member array by typing let member array meaning for each member
of the group.
And we're going to pull out the child here or I guess the object from the child and we're going to get
the child snapshot just like we've done for the username.
Right.
When we pull out the e-mail we go into each user and we pull out the snapshot of their email key and
we get the value then we cast it as a string.
But for the group we're going to pull down the member array.
So we're going to take a group snapshot for the key or for the path members.
All right we're going to cast that as an array of string.
Yes.
So now we have an array of members and that's awesome.
We're getting a morning here cast from data snapshot to unrelated type string always fails.
Oh right sorry.
So a child snapshot.
This is just the name of the key but we want the value.
We want the good stuff inside.
So we need to pull out the value and cast that as an array of string.
So now we have a local array of strings that are the IDs for every member of our group which is really
cool.
So here's what we're going to do.
We're going to get all the groups that we are a part of.
OK.
It doesn't matter if we download all the groups for the entire app because we might not be in every
group.
So what we're going to do is we're going to check to see if the member array includes us.
So let's do that.
If member array contains.
And we're going to pass in.
You ID.
So go ahead and type Auth. off.
Current User dot you ID and it's going to yell at us that we need to unwrap it.
I'll fix that in a second.
But basically what we're doing is we're just saying if this group contains me we're going to move forward.
Because that's the one that really matters is the groups that we're in it doesn't matter if we're in
someone or we show somebody else's group.
So go ahead and click fix to force and wrap the ID from firebase.
And now we're going to go ahead and build our group.
So let's type let a group equals group and we need to pass it some parameters.
Where can we get those firebase we have a title a description.
We have a unique key.
We have a list of members and we have a member count.
So let's go ahead and let's create some variables for those let title equals group right because we
just pulled down this group group got child snapshot for path and for the title The path is called Title.
We need to pull out the value and force cast that as a string.
There we go.
Now we have a title so give it the title.
Next we need to do descriptions so let description equals group.
Child snapshot for path.
Look at firebase.
It's called description so description value as string now we have a description pulled down from firebase
So this we're going to pass it.
Description the key.
The cool thing is all we have to do is type group key and we get the key members.
All we need to do is pass it the members array which we have here.
Pretty neat.
And we're going to go ahead and give the member count which we can get from member array count.
Super duper easy.
Now we have everything that we need.
We have a group we can append it to our group array like so group groups array append.
And it asks for a new element of type group so let's give it one.
Very cool.
So we can get the title the description we have the members which we already pulled down.
We have the members array and we can just access the count to pass in the member count.
So outside of the for loop we're going to use our handler to pass back this array.
So go ahead and type handler and we need an array of group.
We have a groups array.
That's it.
This is all we need to do to download all the groups from our app.
Very cool.
We're using observed single event because when it loads that'll just load all the groups around but
this will not give us that cool real time updating that we want.
So we're going to have to do a little bit of a tweak in our group Visi to make it work the way that
we're hoping.
So go ahead and go into groups AVC and we're not going to reload the table view here.
I'll tell you that much.
But what we're going to do instead is we're going to go ahead and set up view did appear to download
all of those groups add them to a local groups array here and then reload the table view.
It's very similar to what we did in Pheed VC.
We downloaded all the messages.
We set our local message array and then we reloaded our table view.
So in-groups VC we're going to actually do the same thing.
We're going to go ahead and type var groups array and it's going to be an array of group and we're going
to instantiate it like so.
Next we're going to get rid of this view or get rid of the reload data in view to load and we're going
to call you did appear you did appear then call superdog viewed it appear and pass in animated.
Then we're going to do is we're going to call our get all groups functions so type data service instance
get all groups that will download all the groups that involve us.
And then when we return our array of group we'll just call this returned group array groups because
there's more than one group we can go ahead and say self doc groups Saray equals returned groups array.
And this is a great place to reload our table view.
So go ahead and type self groups table view that reload data.
And now our table is going to go ahead and reload every time that we do this.
But remember get all groups is an observed single event function.
It's only going to get called once and executed once if data changes on firebase.
It's not going to get called again because we're only observing at one time not persistently.
So what we're going to do is we're going to set up an observer in did appear to monitor when ever the
group's array is changed.
So let's go ahead and call ref groups.
Oh you know what sorry we have to access our data service first because it's private.
So data service instance ref groups.
And outside of ref groups we're going to go ahead and observe everything.
OK.
Not a single event but everything we're going to observe if it's removed if it's added so that our table
view can be updated in real time.
Go ahead and type value because we're going to be observing for all values added subtracted changed
and we're going to go ahead and return a snapshot.
But we're not going to do anything with it.
What we're going to do is instead pass this in to this closure so that when we are observing all values
when we add a group delete a group change a group it's going to call this return our updated groups
array and reload the table view.
Let's go ahead and let's BuildOn run this and see what happens.
Let's see if it works.
And it's going to give me an error and yell at me.
So rude.
We go are simulator's popping up.
There we go.
Very good.
And as soon as this loads we will try it out to see if our table view is properly working.
All right here we go select groups and oh my we get to crash what's the problem.
So could not cast value of N.S.A..
And as array could not cast a value of type and SNL to MSRA and looks like that air is coming from where
we tried to cast member array as a value of string.
I see the problem.
OK.
Take a look in firebase.
It's called members.
I typed member.
That's the danger of dealing with strings.
Let's build and run and try it again that appeared to be the only issue we couldn't pull down an array
because we did not have the right name.
Embarrassing anyway.
OK let's try it.
Click groups get a table view.
That looks great but it's not downloading information that's problematic.
Let's go back and let's see what the deal is.
Oh guys you know what we did not set up our table view yet to actually pull data from firebase we have
only set it up to show three static cells.
Let's change that.
So instead of returning three rows in section we're going to return the count of groups array.
So let's do that.
Array does count.
That's not what I want.
Groups array count that will return the amount of groups that we've returned from firebase.
Now for configuring the cell we're going to actually go ahead and pull our group from our groups array
for this specific index path.
So we can just call Let group equal groups array and we can pull out the element at the index path for
the particular row.
So if we want the cell at the first row we'll pull out the first index path from our group's array and
that will be our group because remember groups array is of type group.
Now that we have that group we can just go ahead and call group dot title for the title we can add group
description and we can add group member count.
Now if we had 100 groups it would load 100 cells and it would pass in a unique group to each cell and
load the data.
Let's really run this and see how we did we might even add a new group just to see how instantaneously
it adds a new one.
It'll be super cool.
Click groups.
There is not heard.
There are four members.
Looks like it downloaded properly from firebase.
Let's create a new one.
Let's see how we do.
How about devs slopes.
Learn to code with us.
Very cool.
All right.
Let's add some folks let's add Chuck let's add Harvey Dent let's add Mark and Evan and her money.
OK.
There we go.
Click done and look at that.
Learn to code with us.
It was instant.
Did you see how instant that was.
It's downloading from firebase.
It's showing the amount of members our groups are working guys.
This is amazing.
So we can now download groups from firebase we can import them into our table view and show them in
real time in the next video we're going to build group BVC where you're basically going to set up a
similar window to the feed we see here which is public but it's going to be per group it's going to
only allow members of the group to message each other and it's going to show all the messages for that
is going to be super cool.
Let's go ahead let's head over that way and let's build it out.