[英]Updating UI components from an async callback (dispatch_queue)
how can i update GUI elements with values from a queue? 如何使用队列中的值更新GUI元素? if i use async queue construct, textlable don't get updated.
如果我使用异步队列构造,textlable不会更新。 Here is a code example i use:
这是我使用的代码示例:
- (IBAction)dbSizeButton:(id)sender {
dispatch_queue_t getDbSize = dispatch_queue_create("getDbSize", NULL);
dispatch_async(getDbSize, ^(void)
{
[_dbsizeLable setText:[dbmanager getDbSize]];
});
dispatch_release(getDbSize);
}
Thank you. 谢谢。
As @MarkGranoff said, all UI needs to be handled on the main thread. 正如@MarkGranoff所说,所有UI都需要在主线程上处理。 You could do it with performSelectorOnMainThread, but with GCD it would be something like this:
你可以用performSelectorOnMainThread来做,但是使用GCD它会是这样的:
- (IBAction)dbSizeButton:(id)sender {
dispatch_queue_t getDbSize = dispatch_queue_create("getDbSize", NULL);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(getDbSize, ^(void)
{
dispatch_async(main, ^{
[_dbsizeLable setText:[dbmanager getDbSize]];
});
});
// release
}
Any UI update must be performed on the main thread. 必须在主线程上执行任何UI更新。 So your code would need to modified to use the main dispatch queue, not a queue of your own creation.
因此,您的代码需要修改才能使用主调度队列,而不是您自己创建的队列。 Or, any of the
performSelectorOnMainThread
methods would work as well. 或者,任何
performSelectorOnMainThread
方法也可以。 (But GCD is the way to go, these days!) (但是GCD是最好的选择,这些天来!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.