Sethmr
Sethmr

Reputation: 3074

What is a good way to handle unreceived responses from api calls in Swift?

I am about to release multiple Apps, and I have one issue that I know I want to address before release. When I make an API call and never receive a response, my Apps will not be able to function. They are very dependent on having internet access to function at all. How do I setup some form of delay before I send another call? How do I decide how long I need to wait as many of the API calls vary in timelines of their normal response times?

I am definitely most interested in best practices, but am slightly confused as to any good method of doing this in that when you don't receive a response, there is no trigger. I suppose I could setup a timestamp and check the difference between if and a timestamp of now using an NSTimer... but is this even a good way of handling this? Most methods I can currently think of feel bulky and wrong.

Upvotes: 0

Views: 45

Answers (1)

Luke
Luke

Reputation: 5076

Take a look at Grand Central Dispatch and DispatchSemaphores (in swift 3). You can use semaphores to wait for an operation to complete with a specified timeout. If the operation doesn't complete normally and it times out it will give you a specific result which you can then use to handle it in your code.

Upvotes: 1

Related Questions