Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's use the operation we just defined to get detail information about a particular restaurant and display it in the detail view.
-
0:00
Navigate to the YelpSearchController class, if you're not already here.
-
0:07
And in here, we're going to create an instance of an operation queue to manage
-
0:11
our new operations.
-
0:12
So we'll do this up at the top.
-
0:14
And this is getting kinda messy.
-
0:17
But I'll add it to the very bottom above the isAuthorized property,
-
0:23
so in here we'll say let queue = OperationQueue.
-
0:27
We're not gonna customize this queue in any way.
-
0:30
Next, let's create an instance of the operation and add it to the queue.
-
0:34
Remember, we only want to do this once we know which business the user cares about.
-
0:39
Which is in the UITableViewDelegate method we added earlier,
-
0:42
once the user has tapped a row.
-
0:44
So in the jump bar, we're going to look for
-
0:46
that section we marked off, UITableViewDelegate.
-
0:48
And the method is tableView_didSelectRowAt.
-
0:52
So in here, we're simply performing the segue, or
-
0:56
calling it the performSegue method.
-
0:57
Let's add a few more lines of code.
-
1:00
Since the initializer requires a business, the operation class initializer,
-
1:04
let's start by getting the business from the data source.
-
1:07
So, let business = dataSource.object(at: indexPath).
-
1:15
Using this, and the client that we have in the stored property,
-
1:19
we can create an operation and add it to the queue.
-
1:21
And we'll say, let operation = YelpBusinessDetailsOperation,
-
1:27
takes a business, pass it in, and we'll say self.client here.
-
1:33
After that, all we need to do is queue.addOperation(operation).
-
1:39
Pretty easy.
-
1:40
Now that we've added the operation to the queue,
-
1:42
all we need to do now is to indicate what we'd like to do on the completion
-
1:47
of the operation by assigning a closure to the completion block property.
-
1:51
And we'll do that before we add it to the queue.
-
1:53
So, operation.completionBlock = and we're gonna assign it a closure.
-
1:59
Since the operation could be executed on a background fit most definitely will be,
-
2:04
we'll make sure we execute this code on the main thread.
-
2:06
So we'll say DispatchOueue.main.async.
-
2:13
We'll update the data source in here with the updated business.
-
2:16
Remember that when we complete the operation,
-
2:20
we've updated that business with new details.
-
2:23
We aren't extracting it and returning it in some way.
-
2:25
So in here we're going to get this very same business out because the one we pass
-
2:29
in is the one that's updated.
-
2:30
And then we'll add it back to the data source.
-
2:32
So in here we'll say self.dataSource.update object,
-
2:38
the object is the business we passed in.
-
2:41
The indexPath is the indexPath of the current row.
-
2:45
After that, we're going to move this call to performSegue withIdentifier.
-
2:50
So after we've updated the dataSource, then we'll call performSegue again.
-
2:55
At this point, we're about to segue to the detail view controller.
-
2:59
And we have an updated business instance with details about ours and
-
3:04
other stuff we need to populate the detail view.
-
3:07
We still have to hand this model over to the detail controller.
-
3:11
And the way we do that is how we always do it, by overriding prepare for segue.
-
3:15
Now, there's an error here and that's because I need to say self..
-
3:18
Okay, so we already have the prepare for segue method in here.
-
3:22
And that's under the Navigation header.
-
3:25
Let's prepare for a sender.
-
3:27
So let's go there, and inside here we're already checking for the right identifier.
-
3:32
And we'll start,
-
3:33
step one is to figure out which business we need to give the detail controller.
-
3:37
And again, we'll just ask the tableView which row we selected.
-
3:40
So we'll say if let indexPath.
-
3:45
= tableView.indexPathForSelectedRow.
-
3:52
Once we have that the rest is trivial.
-
3:54
So again we'll get the business out of the dataSource,
-
3:57
dataSource.object(at: indexPath).
-
4:01
And then we'll assign it to the detail controller.
-
4:03
So we'll say let detailController = segue.destination as,
-
4:10
and we'll just forecast it to our type, YelpBusinessDetailController.
-
4:18
Once we have that, detailController.Business,
-
4:21
this should all be familiar to you, = business.
-
4:26
Now, let's run the app.
-
4:29
I need to select a simulator.
-
4:31
Here we go.
-
4:36
Hopefully I selected the right simulator, the same one that I used last time.
-
4:41
Otherwise, we will not have the access token or the location and
-
4:44
we need to do all that again.
-
4:45
But that shouldn't take long.
-
4:48
Wow, this is tiny.
-
4:49
Okay let's, there we go.
-
4:52
Okay, so I'm going to type coffee here to restrict it,
-
4:56
we'll get something, we'll click it.
-
5:00
And now look at that.
-
5:01
We have all the detail that we've expected, right?
-
5:04
So we have the name here,
-
5:06
we have the category, we have the hours it's open today, and whether it's open.
-
5:10
It's open right now because It's past 7 AM.
-
5:13
But we're not taking into account here the time difference which is
-
5:17
a bit more complicated.
-
5:18
Remember this is in Cupertino, I'm on the eastern coast of the US, so
-
5:22
clearly it's not 7 AM in Cupertino yet, I don't think.
-
5:26
Okay, now to complete this view we still need to display reviews down here,
-
5:32
so let's do that in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up