Return values from completion handler - swift

I want to return the values from an api call.
The call to my api class (I want to get the values in res):
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let t = Api_test();
let res = t.getSomething();
print(res)
}
}
The api class:
import Foundation
class Api_test {
func getAllStations(completionHandler: (response : XMLIndexer) -> ()) {
getRequest { result in
completionHandler(response: SWXMLHash.parse(result))
};
}
func getRequest(completionHandler: (result: NSData) -> ()) {
let baseUrl = "http://api.test.com"
let request = NSMutableURLRequest(URL: NSURL(string: baseUrl)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "GET"
let task = session.dataTaskWithRequest(request) {
(data, response, error) in
if data == nil {
print("dataTaskWithRequest error: \(error)")
return
} else {
completionHandler(result: data!)
}
}
task.resume()
}
}
Everything works as thought, but I'm stuck at the point to return the values back to the getSomething function. The data is in xml format. How can I get the result set as returned values in the res (viewDidLoad)?

NSURLSession is a fully asynchronous networking API so ideally your view controller should operate correctly and not wait for the data to be returned from the network.
You have three options here:
You can pass a completion block to getSomething and have it pass the result to the block:
func getSomething(completionHandler: (result: XMLIndexer) -> ()) {
getRequest { result in
completionHandler(result: SWXMLHash.parse(result))
}
}
override func viewDidLoad() {
...
t.getSomething { res in
print(res)
}
}
If you desperately need the XML data in hand before view is displayed onto screen, you can make the main thread wait till network operation finishes executing. You can use dispatch_semaphore_t:
func getSomething() -> XMLIndexer? {
var xml: XMLIndexer? = nil
let semaphore: dispatch_semaphore_t = dispatch_semaphore_create(0)
getRequest { result in
xml = SWXMLHash.parse(result)
dispatch_semaphore_signal(semaphore)
}
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
return xml
}
Last option is, you can use another 3rd party that does the parsing synchronously. There is a great one named Ono:
var error: NSError?
let xml = ONOXMLDocument(data: result, error: &error)

Related

How to wait until data from network call comes and only then return value of a function #Swift

I have a service class that makes an api call and stores data into its property. Then my interactor class have a method where I want to make service class api call and when data will be stored - return it. I tried myself to handle this with completion handler and dispatch group, but (I suppose I just missing something) this didn't work. I would be very appreciated if you help me to deal with this problem. Thanks in advance!
Service class:
class PunkApiService{
var beers = [Beer]()
func loadList(at page: Int){
//MARK: - Checks is URL is valid + pagination
guard let url = URL(string: "https://api.punkapi.com/v2/beers?page=\(page)&per_page=25") else {
print("Invalid URL")
return
}
//MARK: - Creating URLSession DataTask
let task = URLSession.shared.dataTask(with: url){ data, response, error in
//MARK: - Handling no erros came
guard error == nil else {
print(error!)
return
}
//MARK: - Handling data came
guard let data = data else{
print("Failed to load data")
return
}
do{
let beers = try JSONDecoder().decode([Beer].self, from: data)
self.beers.append(contentsOf: beers)
}
catch{
print("Failed to decode data")
}
}
task.resume()
}
And Interactor class(without completion handler or dispatch group):
class BeersListInteractor:BeersListInteractorProtocol{
private var favoriteBeers = FavoriteBeers()
private var service = PunkApiService()
//MARK: - Load list of Beers
func loadList(at page: Int) -> [Beer]{
service.loadList(at: page)
return service.beers
}
Added: my attempt with completion handler
var beers: [Beer]
func loadList(at page: Int, completion: ()->()){
service.loadList(at: page)
completion()
}
func completion(){
beers.append(contentsOf: service.beers)
}
loadList(at: 1) {
completion()
}
This is what async/await pattern is for, described here. In your case both loadList functions are async, and the second one awaits for the first one:
class PunkApiService {
func loadList(at page: Int) async {
// change function to await for task result
let (data, error) = try await URLSession.shared.data(from: url)
let beers = try JSONDecoder().decode([Beer].self, from: data)
...
return beers
}
}
class BeersListInteractor: BeersListInteractorProtocol {
func loadList(at page: Int) async -> [Beer]{
let beers = await service.loadList(at: page)
return service.beers
}
}
See a good explanation here
I think that you were on the right path when attempting to use a completion block, just didn't do it correctly.
func loadList(at page: Int, completion: #escaping ((Error?, Bool, [Beer]?) -> Void)) {
//MARK: - Checks is URL is valid + pagination
guard let url = URL(string: "https://api.punkapi.com/v2/beers?page=\(page)&per_page=25") else {
print("Invalid URL")
completion(nil, false, nil)
return
}
//MARK: - Creating URLSession DataTask
let task = URLSession.shared.dataTask(with: url){ data, response, error in
//MARK: - Handling no erros came
if let error = error {
completion(error, false, nil)
print(error!)
return
}
//MARK: - Handling data came
guard let data = data, let beers = try? JSONDecoder().decode([Beer].self, from: data) else {
completion(nil, false, nil)
return
}
completion(nil, true, beers)
}
task.resume()
}
This is the loadList function, which now has a completion parameter that will have three parameters, respectively the optional Error, the Bool value representing success or failure of obtaining the data, and the actual [Beers] array, containing the data (if any was retrieved).
Here's how you would now call the function:
service.loadList(at: page) { error, success, beers in
if let error = error {
// Handle the error here
return
}
if success, let beers = beers {
// Data was correctly retrieved - and safely unwrapped for good measure, do what you need with it
// Example:
loader.stopLoading()
self.datasource = beers
self.tableView.reloadData()
}
}
Bear in mind the fact that the completion is being executed asynchronously, without stopping the execution of the rest of your app.
Also, you should decide wether you want to handle the error directly inside the loadList function or inside the closure, and possibly remove the Error parameter if you handle it inside the function.
The same goes for the other parameters: you can decide to only have a closure that only has a [Beer] parameter and only call the closure if the data is correctly retrieved and converted.

Swift: Testing a URLSession called with delegates

I'm trying to do the unit tests for my app.
I've this function preparing the request
func getWeatherDataAtLocation() {
let WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather"
let weatherAPI = valueForAPIKey(named:"weatherAPI")
let lat = String(locationService.latitude)
let lon = String(locationService.longitude)
do {
try networkService.networking(url: "\(WEATHER_URL)?APPID=\(weatherAPI)&lon=\(lon)&lat=\(lat)", requestType: "weather")
} catch let error {
print(error)
}
}
I've a service class networkservice processing the network request :
class NetworkService {
var weatherDataDelegate: WeatherData?
var session: URLSession
init(session: URLSession = URLSession(configuration: .default)) {
self.session = session
}
func networking(url: String, requestType: String) {
var request = URLRequest(url: requestUrl)
request.httpMethod = "GET"
var task: URLSessionDataTask
task = session.dataTask(with: request) { (data, response, error) in
switch requestType {
case "weather":
do {
let weatherJSON = try JSONDecoder().decode(WeatherJSON.self, from: data)
self.weatherDataDelegate?.receiveWeatherData(weatherJSON)
} catch let jsonErr {
print(jsonErr)
}
case // Other cases
default:
print("error")
}
}
task.resume()
}
}
Then i've the delegate running this function to update the JSON received
func receiveWeatherData(_ data: WeatherJSON) {
self.dataWeather = data
do {
try updateWeatherDataOnScreen()
} catch let error {
print(error)
}
}
The issue is I've no idea how I can write some code to test this and all the ressources I find is to test with a callback, any idea?
So there are mutliple steps in this.
1: Create a mocked version of the response of exactly this request. And save it in a json file. Named like weather.json
2: Once you have done that you want to add an #ifdef testSchemeName when executing request. And tell it to tell your function called networking() to read from a file named "\(requestType).json" instead of making the request.
Optional, more advanced way:
This actually intercepts your request and send you the file data instead. A bit more advanced, but your testing gets 1 level deeper.

Getting data out of a completion handler and into a tableView

I've been trying to understand this process, I've done a lot of reading and it's just not clicking so I would be grateful if anyone can break this down for me.
I have a method to retrieve JSON from a URL, parse it, and return the data via a completion handler. I could post code but it's all working and I (mostly) understand it.
In my completion handler I can print the data in the console so I know it's there and everything good so far.
The next bit is what's tripping me up. While I can use the data in the completion handler I can't access it from the view controller that contains the handler.
I want to be able to pass tableData.count to numberOfRows and get "Use of unresolved identifier 'tableData'"
I'd really appreciate it if anyone can lay out what I need to do next. Thanks!
Edit: adding code as requested
Here is my completion handler, defined in the ViewController class:
var tableData: [Patient] = []
var completionHandler: ([Patient]) -> Void = { (patients) in
print("Here are the \(patients)")
}
in viewDidLoad:
let url = URL(string: "http://***.***.***.***/backend/returnA")
let returnA = URLRequest(url: url!)
retrieveJSON(with: returnA, completionHandler: completionHandler)
Defined in Networking.swift file:
func retrieveJSON(with request: URLRequest, completionHandler: #escaping ([Patient]) -> Void) {
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: request as URLRequest) {
// completion handler argument
(data, response, error) in
// completion handler
guard let data = data else {
print("Did not recieve data")
completionHandler([])
return
}
do {
let decoder = JSONDecoder()
let Patient = try decoder.decode(Array<Patient>.self, from: data)
// print(Patient)
completionHandler(Patient)
}
catch let err {
print("Err", err)
completionHandler([])
}
}
task.resume()
}
I also have a struct defined called Patient but I won't post that as it's very long and just a simple struct matching the JSON received.
First of all, when you use closure, you should consider strong reference cycle.
let completionHandler: ([Patient]) -> Void = { [weak self] patients in
guard let strongSelf = self else { return }
strongSelf.tableData = patients // update tableData that must be used with UITableViewDataSource functions.
strongSelf.tableView.reloadData() // notify tableView for updated data.
}
You are not populating the array(tableData) in the closure:
var completionHandler: ([Patient]) -> Void = {[weak self] (patients) in
print("Here are the \(patients)")
self?.tableData = patients
}
var tableData: [Patient] = []
var completionHandler: ([Patient]) -> Void = { (patients) in
self.tableData = patients
self.tableView.reloadData()
//make sure your tableview datasource has tableData property used
}

Swift3 Cannot assign value of type '()' to type '[Version]'

Hello I am having trouble calling my methods to the controller properly as I am getting this error Cannot assign value of type '()' to type '[Version]'. I need help fixing this, thanks.
Swift 3 Method:
var versions : [Version] = []
func isActiveVersion() -> Bool {
let api = versionAPI()
versions = api.getVersionFromAPI(completion: ({_ in }))
for version in versions{
if version["version"] == "1.0.0" {
return true
}
}
}
Swift 3 Call
public class versionAPI {
var versions : [Version] = []
//---------------------------------
// MARK: ENDPOINTS
//---------------------------------
let getVersionEndPoint = "http://127.0.0.1:3000/api/v1/versions"
//---------------------------------
// MARK: REQUESTS
//---------------------------------
func getVersionFromAPI(completion: #escaping ([Version]) -> Void){
let url = URL(string: getVersionEndPoint)
let task = URLSession.shared.dataTask(with: url! as URL) { data, response, error in
guard let data = data, error == nil else {
completion([])
return
}
print(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!)
self.parseVersionsToJSON(data: data)
completion(self.versions)
}
task.resume()
}
func parseVersionsToJSON(data: Data) {
do {
self.versions = []
if let json = try JSONSerialization.jsonObject(with: data) as? [[String:Any]] {
for dic in json {
let version = Version()
version.version = dic["version"] as! String
version.active = dic["active"] as! Bool
self.versions.append(version)
}
}
}
catch{
}
}
}
Your function getVersionFromAPI sets up an asynchronous task and then immediately returns before that task completes, returning void to its caller. This is why you get the error you report.
The [Version] value produced by the asynchronous task is passed by that task to the completion function passed to getVersionFromAPI. The completion function you pass {_ in } does nothing, so the list of versions is simply discarded.
You cannot simply call an asynchronous task, which will complete at some future time, from a synchronous task, getVersionFromAPI in your case, and have that asynchronous task somehow become synchronous and return its future result immediately.
You need to study asynchronous design. Then either redesign your code so the task done by getVersionFromAPI is itself asynchronous, or use one of the standard techniques to block your synchronous method from proceeding until the asynchronous one has completed.
If after revising your design you have trouble with your code ask a new question, showing your code, and someone will undoubtedly help you.
HTH
versions = api.getVersionFromAPI(completion: ({_ in }))
getVersionFromAPI does not return anything. Declare a global struct then pass the data into it and use DispatchQueue when finished to post a NotificationCentre
do {
GlobalStruct.versions = []
if let json = try JSONSerialization.jsonObject(with: data) as? [[String:Any]] {
for dic in json {
let version = Version()
version.version = dic["version"] as! String
version.active = dic["active"] as! Bool
GlobalStruct.versions.append(version)
}
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "gotIt"), object: nil)
}
}
}
your Swift 3 ViewController should have the following:
var versions: [Version] = []
func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(myFunction), name: NSNotification.Name(rawValue: "gotIt"), object: nil)
let api = versionAPI()
api.getVersionFromAPI()
super.viewDidLoad()
}
func myFunction() {
versions = GlobalStruct.versions
if isActiveVersion {
.....
}
}

Requests and handlers in swift

I need to get data from url. For it I have get method in "HTTPClient" class.
func getRequest(url: String, parameters: String = "", completion: (NSData) -> ()) {
var request = NSMutableURLRequest(URL: NSURL(string: url)!)
if parameters != "" {
request = NSMutableURLRequest(URL: NSURL(string: url + "?" + parameters)!)
}
request.HTTPMethod = "GET"
request.HTTPShouldHandleCookies = true
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
if error != nil {
print(error?.localizedDescription)
return
} else {
completion(data!)
}
}
task.resume()
}
But when I call it from "MainService" class I don't get data in handler. (I don't come in handler)
func getAvailableCoins() -> [Coin]? {
var coins = [Coin]?()
httpClient.getRequest("http://shapeshift.io/getcoins", completion: { data in
coins = self.shapeShiftService.availableCoins(data)
})
return coins
}
What problem can be there?
I just realised that you return from your getAvailableCoins() method BEFORE your handler gets a chance to be called. Consequently you return an empty array.
So basically the thing is that getAvailableCoins() is an asynchronous operation and you can't use return statement to return result. What you could do is declare a special method and call it when your coins are fetched:
func getAvailableCoins() {
var coins = [Coin]?()
httpClient.getRequest("http://shapeshift.io/getcoins", completion: { data in
coins = self.shapeShiftService.availableCoins(data)
coinsAreReady(coins)
})
}
func coinsAreReady(coins: [Coin]?) {
//do what you need with your coins here
}
Now coinsAreReady will be called when your data is loaded and you can work with it from there.