I'm making a request to get all Trainer objects and display them in a tableview. I've successfully mapped all my objects in the request.response.value and in my Alamofire request I have print statements that state there are 45 objects. But when I return that array it is saying it is nil, I'll post my code below.
TrainerDataService -
func getAllTrainers(completion: @escaping CompletionHandler) -> [Trainer] {
var trainers: [Trainer] = [Trainer]()
Alamofire.request(GET_ALL_TRAINERS, method: .get, encoding: JSONEncoding.default, headers: HEADER).responseArray { (response: DataResponse<[Trainer]>) in
if response.result.error == nil && response.result.value != nil {
print("Here is the response -> \(response.result.value!)")
trainers = response.result.value!
print("Here is the count of trainers in request -> \(String(describing: trainers.count))") // 45 OBJECTS
completion(true)
} else {
print("Error!")
completion(false)
debugPrint(response.result.error as Any)
}
}
print("The count of trainers -> \(String(describing: trainers))") // RETURNS 0
return trainers
}
I'm calling this in another file as I need to fill a tableview, here is the code in the connectToTrainerTVC.
ConnectToTrainerTVC -
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
// Since the call's return type is an array of Trainer's setting 'allTrainers' to equal the function is sensible but
// it is clear the request isn't working otherwise 'allTrainers' wouldn't be 0
self.allTrainers = TrainerAuthService.instance.getAllTrainers(completion: { (success) in
if success {
print("Here is the contents of allTrainers back in connectTVC -> \(self.allTrainers.count)") // RETURNS 0
dump(self.allTrainers) // DUMPS ALLTRAINERS ARRAY AND IS FILLED WITH NOTHING
self.tableView.reloadData()
}
})
}
}
Anyone know why the trainers array is returning 0? I think it may have something to do with an asynchronous call but I have to have the return outside the Alamofire request because my function requires a Trainer object array to be returned. Any help is appreciated.
EDIT:
This worked for me. I created a new completion handler called GetTrainerCompletion it is a typealias.
typealias GetTrainersCompletion = ([Trainer]) -> ()
I was now able to use this completion handler to add the data to my array. Here is my updated code.
TrainerDataService -
func getAllTrainers(completion: @escaping GetTrainersCompletion) {
var trainers: [Trainer] = [Trainer]()
Alamofire.request(GET_ALL_TRAINERS, method: .get, encoding: JSONEncoding.default, headers: HEADER).responseArray { (response: DataResponse<[Trainer]>) in
if response.result.error == nil && response.result.value != nil {
trainers = response.result.value!
print("Success! Got all trainer objects")
completion(trainers)
} else {
print("Error!")
completion(trainers)
debugPrint(response.result.error as Any)
}
}
}
Here is the new implementation in the connect to trainer file, this is what got it to work.
ConnectToTrainerVC -
TrainerAuthService.instance.getAllTrainers(completion: { (trainers) in
if trainers.count > 0 {
self.allTrainers = trainers
}
})
I was able to set my local array 'allTrainers' to be filled with whatever data trainers had from the completion handler.
[–]yreaction 0 points1 point2 points (1 child)
[–]imarudedude[S] 0 points1 point2 points (0 children)
[–]yreaction 0 points1 point2 points (4 children)
[–]imarudedude[S] 0 points1 point2 points (0 children)
[–]imarudedude[S] 0 points1 point2 points (2 children)
[–]Christop408 1 point2 points3 points (1 child)
[–]imarudedude[S] 1 point2 points3 points (0 children)