Reputation: 2981
in my app I want to set the text of an UILabel
. The text comes from a JSON-object. I add my UILabel
to my storyboard, set the IBOutlet
and call my async-method to get my JSON-object.
In the response-method I set the text of the UILabel
. But the text change needs some seconds.
When the response comes I print it to the console. There I can see, that the delay doesnt comes from the async-method.
The response comes, I can see it in the console. Wait some seconds than the UIlabel
changes.
I dont understand this behaviour, is there a trick to refresh the UIlabel
instantly?
some code:
@IBOutlet weak var label_news: UILabel!;
override func viewDidLoad() {
super.viewDidLoad()
self.label_news.text = "CHANGE";
rcall.GetNews_GET_NewsResponse_0(self.NewsResponseHandler);
}
func NewsResponseHandler(resp:NewsResponse!){
self.label_news.text = resp.NewsText;
println(resp.NewsText);
}
Sorry if this is a beginner question, swift and storyboards are totally new for me.
best regards
Upvotes: 2
Views: 2389
Reputation: 7802
c_rath's answer is correct. In swift 3 the syntax was changed (yet again) to
DispatchQueue.main.async {
self. label_news?.text = resp.NewsText
}
Upvotes: 0
Reputation: 3628
Like what Rob stated in the comment, all UI changes need to be done on the main thread. I haven't implemented it in Swift yet, but the Objective-C and what I'm assuming would be the Swift is below...
Objective-C
dispatch_async(dispatch_get_main_queue(), ^{
self.label_news.text = resp.NewsText;
});
Swift:
dispatch_async(dispatch_get_main_queue()) {
self.label_news.text = resp.NewsText;
}
Upvotes: 9