Ezra Henley
Ezra Henley

Reputation: 349

Implementation of a loading spinner in swift

I'm attempting to show a loading spinner when I'm doing some network calls when my app first starts up from being closed. These network calls usually take a very small amount of time because they are GETs on a json string and some processing on them, but if they take longer than usual, I don't want my users trying to maneuver in the app without the data they need being there. So, I'm trying to show a spinner when these calls are going on. But the spinner never shows up. I had this working before I changed a lot of stuff, and now it's not working again, and I can't for the life of me figure out why.

Here's my viewDidLoad() method in my HomeViewController, where this information is pulled from the API and loaded into CoreData.

override func viewDidLoad() {

        super.viewDidLoad()
        self.showSpinner(onView: self.view)
        let teamsByConferenceNetworkManager = TeamsByConferenceNetworkManager()
        teamsByConferenceNetworkManager.getTeamsByConference(completion: { (data, error) in
            guard let data = data else {
                os_log("Could not unwrap teamsByConference data in LoginViewController.viewDidLoad()", type: .debug)
                self.removeSpinner()
                let _ = UIAlertAction(title: "Network unavailable", style: .cancel, handler: { (alert) in
                    alert.isEnabled = true
                })
                return
            }
            let dataModelManager = DataModelManager.shared
            DispatchQueue.main.sync {
                dataModelManager.loadTeamNamesByConference(teamNamesByConferenceName: data)
                dataModelManager.loadGamesFromCoreData()
            }
            if let _ = dataModelManager.allGames {
                self.removeSpinner()
                return
            } else {
                let gamesNetworkManager = GamesNetworkManager()
                gamesNetworkManager.getGames { (data, error) in
                    guard let data = data else {
                        os_log("Could not unwrap games data in LoginViewController.viewDidLoad()", type: .debug)
                        self.removeSpinner()
                        let _ = UIAlertAction(title: "Network unavailable", style: .cancel, handler: { (alert) in
                            alert.isEnabled = true
                        })
                        return
                    }
                    DispatchQueue.main.sync {
                        dataModelManager.loadGames(gameApiResponses: data)
                    }
                }
            }

        })
        self.removeSpinner()

    }

Upvotes: 1

Views: 1826

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You need to remove this

                DispatchQueue.main.sync {
                    dataModelManager.loadGames(gameApiResponses: data)
                }
            }
        }

    })
    self.removeSpinner(). <<<<<< this line 

}

as the call is asynchronous and you remove the spinner directly after you add it with self.showSpinner(onView: self.view)

Upvotes: 2

Related Questions