Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Swift 1
  • Overview
  • Transcript

3.3 Operation Queues and Concurrency

If you have a lot of data in your app, your user might also be able to start a lot of requests. In this lesson I'll show you how you can limit request creation, while also making data handling more efficient with concurrent requests.

3.3 Operation Queues and Concurrency

Hi, and welcome back to go further with Swift, where we build the weather app for iOS. In this lesson, we are going to look at operation cues and how to use them to manage your network connections. We already learned that the task doesn't necessarily run on the main cue. If you look at the debug navigator, you can see that the request runs on thread five which contains the que for our network request. There is also another thread that supports the network request, so it's quite spread out. One of the reasons to use a custom cue is to make your request concurrent or to limit the number of requests that are made at the same time. Some argue that you could just use HTTP maximum connection per host. Which is true for a single host, but if you are using multiple domains, for instance, because your data is uncontrollably spread out, this setting won't work. Let's create an operation queue constant in the class and set it under session as a delegate queue. If you use nil here, you'll get a [INAUDIBLE] where only one request is made at the same time. Let me demonstrate that in a few controller. I'm going to make three calls to the API to be able to tell them apart, I'm using the coordinates as an index. To see this in the console, I'm going to print the query parameters in the response block. As you can see, those get processed in the order they were queued in. Let us use our operation queue instead and set the concurrency option to two. In this case, the second request ran faster than the first one. But number three and the real request was started later than the first two. Before we finish the networking part, I wanted to add another feature to the app. Which is showing a network connectivity indicator whenever there is a request being made. I'm going to add a network indicator account variable to the clause and set it to zero. It gets a good set callback which shows or hides the network indicator under shared UI application depending on the count. Before I make the request, I'm going to increment the count by one. Since plus plus isn't available in Swift anymore, we have to use plus equals one. Everywhere the completion block is called, we have to decrement the count. To avoid raised conditions, let's set it on the main queue. Incrementing is done there as well. To be able to see it, let's change the location to bicycle ride since it will constantly update the location and make request to the server. Now you know how to manage network connections with an operation queue. In the next lesson, we will create our first custom interface component. See you there.

Back to the top