0:26
All right, so I'm just going to start us off with this method,
and make sure that I have my curly braces balanced, just so
we don't get distracted by errors as we're going through it, okay.
0:47
So here we are, and we're inside of our fetch users
with hobby function now that we're creating.
So look at the function signature here, we say func fetchUsersWithHobby, and
then the argument is called hobby, and then so type hobby, it makes sense, right?
So now the first thing we want to do is make sure that we don't
run into bad conditions.
So I have a guard statement here, and
it says go get the CurrentUserId out of our standardUserDefaults, right, so
NSUserDefaults.standardUserDefaults().val- ueForKey,
the key name CurrentUserID that we used when we stored it in there.
And again, remember, spelling and capitalization count.
1:42
If it is, then I can look at the number of characters in that string.
So characters.count and make sure that the user ID actually
has some letters and numbers, actually has some characters in that string.
So you can make sure that it's not an empty string, because it could be none,
nil, but be an empty string, and so that's not a valid user ID either.
So now that I made sure I have a current user ID, and
we have more than zero characters in that string,
we have a pretty good chance that we have a valid current user ID now.
But if that's not the case, then we'll get inside of this guard statement.
So we need a good user ID to go to the server and
say, hey, server, give me some lists.
Here's my user ID, so that you know that I have permission to ask you for things.
2:45
and that way, we can give you users who share your hobby that are near you.
And to make sure that you get some results or help make sure we've defined
near you as within 1,000 miles of you, which is a pretty big scope,
but want to make sure that you can be pretty successful.
So define near you as within 1,000 miles of you, and also,
since we are passing in your lat and long, we take that opportunity to go update your
lat and long inside of the user's table in the database in the backend.
So if you change your location and you press a hobby
on the scene with the map in it, then your location should also get updated.
It says you're working on this project, maybe you're working on it with a friend,
or maybe you have two devices, or a simulator and a device, and
you want to play with the application a little bit.
Just be aware that your location will get updated when you make this call to go
fetch users with hobby.
Okay, so, now that we've made sure we have
a good user ID, and we can get down here.
Otherwise, if we don't have a good user ID, then we'll just throw up an alert
onto the screen and say, please log in before selecting a hobby.
If you don't have a current user ID, that means you probably didn't log in and
it probably didn't get stored into NSUserDefaults, so when we try to get it
out of UserDefaults here, it'll be nil and will fall into the discard statement.
So it makes sense to say, please log in before selecting a hobby.
We need to add an OK button, so that the user can get rid of that alert.
So, we say, let okAction = UIAlertAction,
title will be Dismiss, so they can dismiss the alert.
The style will be a UIAlertActionStyle.Default, so
it should just look like those little white pop-up boxes that we're all used to.
4:45
And we need a closure for our handler.
So, we can say when they press the dismiss button,
then we call alert.dismissViewController, and we set animated to true.
And then the alert will, in an animated fashion, go away,
and once the alert goes away, we don't need anything else to happen, so
we can just say completion: nil.
All right, so now that we have the OK action defined,
we can add it to the alert, alert.addAction(okAction).
And now that we have our whole alert configured,
we can say self.presentViewController.
The alert is what we're presenting, and we want to present it in an animated fashion,
set this to true.
And once the alert comes onscreen, we don't need anything to happen, so
again, here, completion can be nil.
6:09
And we'll say, requestUser.userId =,
the user ID that we go and pull out of NSUserDefaults,
which, by the way, we've already made sure up here is going to be good, right?
6:22
Now we can say, requestUser.latitude = currentLocation,
if we have it, its coordinate.latitude.
And our requestUser.longitude = same thing,
our current location if we have it, its coordinate.longitude.
Okay, so you saw a little one of my instructions to self.
So [LAUGH] you don't need that comment in the code.
But here now, we're going to go ahead and
call the fetchUsersForHobby data provider function, which we still have to write.
We'll go write it after we do this.
So we'll see a little error for another minute.
But just so we don't get too distracted going to different places,
we're inside of the neighbor z controller.
This is the fetchUsersWithHobby call inside of our view controller, and
we're going to call out to our data provider, right?
So we'll say userDP().fetchUsersForHobby.
We need to know who's making the request, so we put in the requestUser.
And we need to know which hobby we're talking about here.
So we need to put in the hobby.
7:35
And then here's our completion closure, right?
So we use that, again, trailing closure syntax, so
you don't see like the word completion call in and then some stuff.
We can just close this with the parenthesis and
then use an open curly brace.
And we know since completion is going to be the last argument in this list
of arguments for the fetchUsersForHobby function,
we can use this drill enclosure syntax, right?
Okay, so when we go make the call to the server,
it's going to give us back a list of users.
8:11
So we call that returned list of users, right?
We say, requestUser, that's when we send it, and returnedListOfUsers.
That's when we get information back.
Again, uses words request and returned
object in your code to help you keep track of what's going on.
8:32
All right, so now here, we're inside of the closure.
If I return the list of users,
because that's the object that the server is giving us back,
returnedListOfUsers.status.code == 0, that means everything went well.
Then we can say, self.users, remember which is our instance
property we put on this class, = returnedListOfUsers.
All right, so now we put the returnedListOfUsers.users
survey into self.users, so that up here,
when a user taps on one of the cells in the collection view,
we can see if there is anything in the self.users array.
And then if there is something there,
we know that we should remove the map pins before we add the new map pins.
So that's how that works.
[MUSIC]