My goals is to return a value when Alamofire is ready with his tasks. Below script is part of my singleton, that will be called by my viewController. I have looked around and there are examples of where a completion handler is implemented with Alamofire.
Singleton:
func getPeople() -> [Person] {
var persons:[Person] = []
Alamofire.request(requestURL, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
if let json = response.result.value as? Dictionary<String, Any> {
for field in json {
// Reference to person class
let person = Person()
// Properties will be fill in
...
...
// Add properties to the person object
persons.append(person)
}
}
}
return persons
}
Viewcontroller
I will call above function in my ViewController.swift
and add it to an array of People.
var persons = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
let request = requestHandler()
self.persons = request.getPeople()
}
Because Alamofire will be asynchroon, I don't get returned the array of persons. How do I return the value persons
to a function that is called from my viewcontroller.swift
? I am using Swift 3.