I am struggling to seek out a solution that works, mainly have setup a navigationlist which efficiently shows a mannequin sort array of information, solely subject is when loading knowledge from API the content material tries to load to view earlier than knowledge is prepared. mob resolution under errors resulting from UIHostingController
not being run on important thread?
func getGenres(_ completion: @escaping (CompetitionGroup?) -> ()) {
let headers = [
"x-rapidapi-key": "api-key***",
"x-rapidapi-host": "football-web-pages1.p.rapidapi.com"
]
let request = NSMutableURLRequest(url: NSURL(string: "https://football-web-pages1.p.rapidapi.com/competitions.json?embrace=rounds")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (knowledge, response, error) -> Void in
if (error != nil) {
print(error as Any)
} else {
let httpResponse = response as? HTTPURLResponse
do {
let decoder = JSONDecoder()
let comps = strive decoder.decode(CompetitionGroup.self, from: knowledge!)
self.competitions = comps
completion(self.competitions)
}
catch {
print("Error in JSON")
}
}
})
dataTask.resume()
}
Referred to as in SetupUI()
:
non-public func setupUI() {
self.view.backgroundColor = .systemBackground
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", type: .plain, goal: self, motion: #selector(didTapLogout))
self.view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
])
getGenres { (array) in
// Do operation with array
competitionslist = array
let childView = UIHostingController(rootView: ContentView()) // ERROR HERE
self.addChild(childView)
childView.view.body = self.view.bounds
self.view.addSubview(childView.view)
childView.didMove(toParent: self)
}
//competitionslist = getComps()
}
Have tried varied issues that helped others from Stack Overflow however no pleasure, this one at the very least builds.