Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

Asynchronous HTTP Request return Value in Swift

$
0
0

I'm dealing with a design problem right now. I'm writing a Swift App, which has a Storyboard with multiple ViewControllers and a Class HTTPRequests, which performs GET and POST Requests on a RESTful Webservice.

My problem right now is as following. From a view controller, I have to perform a a GET Request on the Webservice to fetch data. The received Data should be displayed through this view controller on the specific view.I see two possibilities to achieve this right now:

  1. Put the Request function into the ViewController. Perform an asynchronous request and update the view in this asynchronous task as soon as data are received.I don't really like this approach for two reasons. First, I don't want the Request function inside the ViewController, because I need the same function in multiple ViewControllers and this leads to code duplication. Second, I don't want to update the view inside the asynchronus task.

  2. Put the Request function into the HTTPRequests (Singleton) Class. Call the requests from a ViewController and get a return value back. With this value, the ViewController is able to update its view.I like this approach, because it's in design concerns "cleaner". My only problem is the return value. How can I return a value from an asynchronous task in Swift?If possible please with code examples.

Or is there a better approach than those two?

Edit:Thanks to your answers and this Blog Post I could successfully implement approach 2 with delegates.


Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>