Reputation: 15
I'm trying to update the title of a button based on a JSON response from an API.
The JSON request as well as the setting of the button title takes place in viewDidLoad in my view controller but the title of the button does not update until after I click on the button.
Anybody experienced this before?
The below takes place inside view did load.
session.dataTask(with: request) {data, response, err in
if let data = data{
var json: Any
do{
json = try JSONSerialization.jsonObject(with: data)
//print(json)
guard let dictionary = json as? [String: Any] else{
print("oops")
return
}
guard let roster = Roster(json: dictionary) else{
print("oops2")
return
}
let member = roster.pickRandomMembers()
self.button.setTitle(member.first_name, for: .normal)
}
catch{
print(error)
}
}
}.resume()
Upvotes: 0
Views: 59
Reputation: 25261
Try this. DispatchQueue.main.async()
will force UI update code inside to be executed on main thread
DispatchQueue.main.async() {
self.button.setTitle(member.first_name, for: .normal)
}
Upvotes: 1