我想在某些内容时显示进度条,但进度条没有更改。 我应该做什么?
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_async(group, queue, ^{
for (int i = 0; i < 100; i++)
{
dispatch_group_async(group, dispatch_get_main_queue(),^{
self.progressBar.progress = (float)i/100;
});
// do big somethings
}
});
答案 0 :(得分:1)
在您的原始问题中,您将progress
设置为0到100之间的整数。如果这是UIProgressView
,则progress
是一个浮点,应该从0到0 1.0。
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_async(group, queue, ^{
for (int i = 0; i < 100; i++)
{
dispatch_group_async(group, dispatch_get_main_queue(),^{
self.progressBar.progress = ((CGFloat) i) / 100.0;
});
// do big somethings
}
});
或者更简单地说,因为你并没有真正使用该组(在全局队列中只有一个操作,并且在处理串行队列时组通常是多余的):
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^{
for (int i = 0; i < 100; i++)
{
dispatch_async(dispatch_get_main_queue(),^{
self.progressBar.progress = ((CGFloat) i) / 100.0;
});
// do big somethings
}
});
如果您仍然没有看到进度视图更新,那么它必须像IBOutlet
一样简单,不能正确连接。确保self.progressBar
不是nil
。尝试将其progress
设置为0.5
,并确保正确反映。
答案 1 :(得分:0)
我建议使用
dispatch_queue_t queue = dispatch_queue_create("com.company.myqueue", NULL);
更新进度状态,因为
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
是并发队列。
答案 2 :(得分:-1)
您应该在主线程中更新进度条:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
// async stuff
dispatch_sync(dispatch_get_main_queue(), ^{
// sync stuff
});
});
dispatch_release(queue);