Everybody this is Caleb with Debb slopestyle and in this video what we're going to do is carry on where
we left off last time which was creating our group.
We can add users now into a chosen user array.
The Done button appears we're going to set up what happens when you tap on that done button today.
So go ahead and open that XCode project and we need to think about where our app currently stands.
Let's pull it up and let's take a look.
If we go in and I search for some users I add in maybe three friends.
And if I want to click done if I want to create this group I'm going to upload it to firebase but as
it currently stands we only have the emails of the users.
And if we're going to be getting there messages later we're going to need to basically pull down their
user ID because if you remember in firebase Let's pull that up.
Every message has content and a Sender ID.
Now every account has an ID and an email.
But right now as it currently stands we have sort of separated our data into only having an email.
So we need to write a function in our data service that's going to allow us to convert an email into
the appropriate ID from firebase.
So let's go into our data service and we're going to write a function called get IDs for user names
and it's going to allow us to pass in an array of usernames and get all of the associated IDs that go
with them.
So go ahead and type phunk get IDs wups only one t IDs for user names.
And go ahead and use an internal parameter there of usernames colon and then the user names of course
all come in as strings.
So that's fine we can leave it at that but we're going to need to return an array of strings as well.
So we're going to use a handler for that because we're using firebase.
So make it in a scaping handler and of course we're going to use a function that returns an empty function
and the value that we're going to pass into the handler is going to be called Let's just call it id
array.
And like I said it's an array of type string.
So what we're going to do now is observe our users reference we're going to observe a single value meaning
it's just going to cycle through one time it's going to return all the IDs for the usernames that we
pass in.
So here we go.
Let's go ahead and let's call refusers.
We're going to observe a single event
and of course the data type is value and we're going to create a snapshot.
Now this is going to be user snapshot and we're basically doing this very similarly to how we've been
doing it in the past with guard let users snapshot equals user snapshot.
Children of all objects.
And we're going to cast it operationally as an array of data snapshot.
Very cool.
Otherwise we're just going to return very.
So that's great.
Now assuming we get to value assuming we have users which if the button is showing that means we have
added users so we definitely will get users back.
Not to mention that we're pulling this from firebase anyway Sorry.
So it's going to pull all the users from firebase.
But you know how we've been creating an array to hold everything doing a for loop appending things to
the for loop and then passing that array back to the handler.
We're going to do the same thing here.
We're going to create an array called Idea array.
So type of var ID array and it's just going to be an empty array.
No not sorry an empty array an array of type string.
My mistake.
So now what we're going to do is we're going to cycle through every user in the user snapshot.
And we're going to compare it against the user names array.
So go ahead and type for user in user snapshot.
So now we're cycling through the user snapshot we're pulling out every user.
And what we're going to do now is we're going to check to see if the user names array contains a certain
user's e-mail.
So go ahead and type if usernames dock contains.
And now we need to get the e-mail value for this user.
If you remember we did that this way right here user child snapshot for path value as string.
Now we're going to go ahead and we're going to copy this and we're going to paste it down here and we're
going to check if the user name user names array contains the e-mail from the user.
OK if it contains it what we're going to do is we're going to go ahead and append it to the idea array
but we're not going to append the e-mail.
We're going to append the key for this user because that's their unique ID.
So go ahead and type ID array append user account key.
OK.
So that is how we're going to fill up our ID array.
Then at the end of the for loop we're going to go ahead and call our handler and we're going to give
it an array of strings which is ideal right.
Pretty cool.
That's all we need to do to get all the IDs for a certain user.
And that's what we're going to do to download all the ideas that we need.
And then after we get all of the ideas we're going to pass them up to firebase to create our group.
So now we need to write the function to create a group.
Now we need to think about a group if we go into our simulator here a group has three things a title
a description and it's also going to have a list of users that are in it.
OK.
So we're going to create a function to create a group go ahead and type phunk create group and we're
going to create a group with a title of course an internal parameter of title we're going to create
a group with a description and description so description I don't think that's spelled right.
No description.
There we go.
So with title and description and we also need to create a group for a set of user IDs and we'll just
say IDs that's going to be an array of string.
OK.
Now that's great and all but we're going to also need a handler to basically let us know when the send
is complete we're also going to need a handler to determine when the group is officially created.
So go ahead and create a handler and it's going to be escaping just like we've been doing.
Be an expert at this by.
By the end.
Go ahead and we're just going to use a boolean to check.
We'll just say a group created of type Boolean and that's what passed pass back to tell wherever we
call this that were done creating a group.
And we'll just use that to dismiss the view controller.
But OK this looks great.
We're ready to create a group now.
We're going to use our group's reference.
We have not used that yet but it's up here and it takes us to the firebase database child called Groups
or at least it will create it if it's not there.
So go ahead and call ref groups.
Whoops ref groups and we're going to go ahead and just every time we create a group it's going to be
created with a random ID because it doesn't really matter.
The idea of the group because the group can be random it's not associated to one particular person.
So go ahead and type child by Auto-ID and then inside that child what we're going to do is update the
child values.
Now it asks for a dictionary and we're going to give it one.
So for this group it's going to need a title and the title value will come from the title that we pass
in.
It's going to need a description and we're going to get that from the description we pass in.
It's also going to need a member's list.
And so what we're going to do is we're going to pass it.
Members.
OK.
And it's going to get that from IDS.
That's the array of strings that we pass in.
And that's what we're going to pass in.
It's not easy it's really easy actually.
So that's great.
And then once that is done we're going to go ahead and just call our handler and we're going to pass
it through OK because our group was then successfully created.
Now I could be using a completion handler here and doing some air handling.
But really the only areas that we're going to run into are errors of Internet connectivity so I'm just
going to assume that you have good enough internet that it's not going to be a problem.
If you want to you can go ahead and add the completion handler and you can say if it's successful handler
returns true.
Otherwise return false and then print the error.
That's up to you.
I'm not going to for the extents and purposes of this video but for now let's go back to create groups
Fisi and let's set up our done button to create a group.
OK let's go back up and we have an IB action here for that done button.
Now we need to think of a few things we need to have some conditions met before we can press done.
And before we can send our group up to firebase and basically let's let's take a look.
So obviously we knew that title we needed description and we need some members right can't just add
it without any members.
But if you remember properly the done button uncheck everybody it's hidden.
If there's no one added so we can't really do that unless it's added anyway so let's just leave it.
We don't need to worry about this or the user's array because the button is hidden and shown based on
that.
But we do need to monitor the text to make sure that there is a title and a description.
So go ahead and we're going to just check if group is a group.
Maybe it's just title title textfield text is not equal to an empty string and description textfield
text is not equal to an empty string.
Now we need to do is we need to download all those user ID is because remember we only have emails we
don't have user IDs so let's go ahead and let's do that by calling data service instance lips instance.
In that instance get ID's for user names.
And where are we going to get those usernames chose chosen user array.
Right.
Because we've already added them all right here we have an array of chosen users.
Now the handler returns an array of string.
And if we think about that that's just an array of all the IDs that we got.
So let's just say IDs array.
And of course that gets returned here.
Now we need to think about this though at this moment we have Marty a great Scott market slopes Harvey
Dent but if you remember the account I'm logged into is Batman at Gotham dot net.
So by default that's not going to show up.
I won't even be in my own group.
So what I need to do is actually add myself to this array and to do that I'm just going to create a
temporary variable that's just going to hold the ideas array and I'll call it var user IDs equals IDs
array.
I'm going to go ahead and call user IDs dot a pen and I'm going to add myself.
So off oops sorry off.
Auth.
do I don't have oh great groups VC does not have firebase.
How funny is that.
All right ad firebase and now go back to user IDs that a append Auth. dot off current user.
And we're going to pass in the ID not the e-mail.
OK and if I build this you'll see it gets an error.
We have to force unwrap it just like so.
And now user IDs has all four of my for or three of my friends and myself.
So that's awesome.
But now I need to create the group on firebase because I just press the done button.
We just created that function so called data service.
Instance WIPs instance create group with Title.
We're going to take title textfield text and description is from description textfield text for user
IDs.
We'll pass it in user IDs that's the one we just pulled down from firebase and added ourselves to.
And now the handler is going to be for.
Let's just say group created capitalized this group created.
All righty.
So very cool.
If the group is created We're going to go ahead and call self dismiss.
True.
And then Knill.
OK so we're just dismissing the view controller.
If the group has been created successfully else we're going to go ahead and just print group could not
be created.
Please try again.
And of course you could always set this so that it shows a Iler controller that's up to you.
But let's go ahead and let's try this let's try to add our group and let's see how we do build and run.
We're getting some errors here.
Oh of course.
Sorry since we're in a closure we need to hold a reference to self or just tell it that it's coming
from this view controller.
So add self dot title textfield a text et cetera and it looks like our build succeeded.
This is great.
Go into groups.
Tap the plus button.
Let's try to let's add some friends here.
Marty.
Great Scott.
Harvey Dent.
Chuck at by more and let's go ahead and let's give our group well let's try it.
Can't do anything unless it has a title.
Let's call this the nerd herd.
The nerdiest nerds around.
Nice.
So now if I select done since I have added a title a description and I have selected people it should
allow me to click done.
All right.
So it worked it dismissed.
That means that our group was created successfully Let's pull up firebase which I believe is hiding
back here.
And let's go look.
OK.
Amazing.
We now have a groups reference and if we look inside has a random ID.
Let's open it up.
We have a description.
The nerdiest nerds around the title Nerd Herd and the members take a look at this.
This is amazing.
We have four users three that I added and one that is myself's.
Remember users Let's find Batman at Gotham that is.
There is.
OK.
So this ID C to D there I m c to d.
The last one in the Iraq because I added myself last.
Very very cool.
So guys we can now successfully create groups in firebase we can upload them in the next video we're
going to do is we're going to set up our group cell so that in here whenever a group is added if we're
added to to a group it'll show up in real time immediately it's amazing.
So in the next video we're going to write that function get our group showing up.
And guys this is this is so cool our app is coming together.
It's starting to look really nice.
Nicely done.
Let's go ahead and move on to the next video where we will set up that table view and pull down all
of our group's.