I have a game that displays two player profiles (randomly chosen out of thousands of players), and the user must choose one of the profiles. When a choice is made, two more random player profiles appear and the user must choose again. The players are chosen from a server-side request and the profiles are built from the JSON returned from the async session:
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
if response == nil {
print ("Bad httpResponse")
}
else {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do {
self.buildPlayers(data: data as NSData?)
}
}
}
}
task.resume()
One of the data parameters that is returned inside the JSON is a playerID (for each player). I then use this playerID to retrieve the respective image (also stored server-side) and update the player's image in the user interface.
However, because this is done using the asynchronous call, it must be done inside the DispatchQueue.main.async() {}
When playing the game, the first time a new player is chosen randomly, the image is retrieved perfectly and displayed exactly as it should. However, all subsequent occurrences where the player is chosen randomly, no image is displayed. One of the options on the game is to open up a new ViewController that has expanded information of the player (including the player's image), and when you do this despite no image being shown inside the game, the player's image is shown inside the player profile view controller, so I know that it is cached properly because it loads instantaneously.
My question: is the DispatchQueue.main.async messing up the way the image is retrieved from the cache? Is there something I am doing incorrectly.
Here is a snippet from the self.buildPlayers(data: data as NSData?) function call:
func buildPlayers(data: NSData?) {
...
let player_id = Int((player_info?["playerID"]?.stringValue)!)!
...
DispatchQueue.main.async() {
// Update Player Info
self.loadHeadshotImage(playerOrder: 1, PlayerID: String(player_id))
And here is the loadHeadshotImage function:
func loadHeadshotImage(playerOrder: Int, PlayerID: String) {
if (playerOrder == 1){
topTeamImg.sd_imageIndicator = SDWebImageActivityIndicator.gray
topTeamImg.sd_setImage(with: URL(string: "http://example.com/PlayerImages/\(PlayerID).png"), placeholderImage: UIImage(named: "loading"))
}
else{
Edit:
One other thing I thought may be an important piece of information to include is that the way the view controller is loaded is not typical and maybe it's having an effect on the timing of the image loaded.
When a choice is made (clicking a button) by the user, the view controller segues to itself. However, because I do not want previous choices to be displayed if the user presses the 'Back' button, I pop the previous view controller off the stack, and then append a new instance:
let whodYouRather = self.storyboard?.instantiateViewController(withIdentifier: "WhodYouRather") as! WhodYouRatherVC
var vcArray = self.navigationController?.viewControllers
vcArray!.removeLast()
vcArray!.append(whodYouRather)
self.navigationController?.setViewControllers(vcArray!, animated: false)
Thank you for any help you can give me in rectifying this bug!