FyZyX
FyZyX

Reputation: 195

Best way to ensure an initial network request is completed before other requests are sent (iOS app)

An app I am working on requires creating a container object on a server and inserting items into that container. I don't want to create the container object until the first item needs to be inserted. However, creating the container object requires some initialization that may take a little time. While that container is still initializing the user can still to send insertion requests that aren't getting handled because the container isn't ready yet. I have two main questions:

  1. Should this be dealt with on the client or server side?
  2. What is the best practice for dealing with kind of this issue?

Essentially, I need to ensure my initial createContainer data task in complete before any insertItem requests are sent.

Addition Information

An insertItem request is sent by clicking on a corresponding tableViewCell. The first tableViewCell a user clicks on sends a createContainer request that creates a container holding the first item.

For a container holding n items, the request should be sent in the following order:

After the first request completes, the remaining n – 1 requests may complete in any order.

My Thoughts

It sounds like I want the createContainer request to be handled synchronously while the insertItem request should be handled asynchronously. I'm not sure if that is the best approach or even how to perform that appropriately, so any guidance would be greatly appreciated.

Upvotes: 2

Views: 1113

Answers (2)

naglerrr
naglerrr

Reputation: 2854

You can use a NSOperationQueue and multiple NSOperations to implement your desired behavior. A NSOperation instance can be dependent on the completion of another NSOperation instance:

dependencies

An array of the operation objects that must finish executing before the current object can begin executing.

For your example this would mean that the insertItem-Operations are dependent on the createContainer operation.

When you add all those operations to a NSOperationQueue your createContainer operation will run first. When it has finished, the other operations will start running as their dependencies are now satisfied. You can also control how many operations you want to run concurrently using maxConcurrentOperationCount on NSOperationQueue.

As you will be using asynchronous API in your NSOperations you will need to implement a ConcurrentOperation and handle the state changes yourself. The API Reference is explaining this in pretty good detail.

Check out the API Reference for NSOperation for further information. There is also a nice NSHipster article on NSOperations.

Upvotes: 5

Siddharth Gupta
Siddharth Gupta

Reputation: 897

Adding to the NSOperationQueue answer, it's sometimes difficult to manually manage all the state changes that an NSOperation requires to handle something asynchronous like a network call.

To simplify that, you can use a Swift Library called Overdrive. It's an amazing library in which you simply subclass a Task class and write your network code in the run() function. And when you're done, you simply call self.finish to finish the task. Here's an example: Just create a simple download task:

Simple Image Download Task

Then, just add it to the queue.

enter image description here

You can also add dependencies between tasks, which basically solves your use case.

enter image description here

Hope this helps.

Upvotes: 0

Related Questions