我在后台线程中运行数学计算。试图在UITextView中实时发布结果。但是,直到后台线程完成后才会显示结果。为什么不?当后台线程繁忙时UITextView不会滚动
我在后台揭开序幕的方法,
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^() {
[self v2];
});
后台线程方法的形式为,
- (void) v2 {
NSString *result;
// ... loop a bunch of times generating lots of results
for (bunch of stuff to compute) {
// If using dispatch_async, nothing is displayed until this method finishes
// If dispatch_sync then it does display and update
result = [self computeNextValue];
dispatch_async(dispatch_get_main_queue(), ^() {
textView.text = result;
});
} // end computation
}
这实际上并没有,直到我开始尝试过太大的问题滚动视图。痛苦地缓慢。所以我创建了一个NSTimer来定期滚动UITextView。但是,即使计时器弹出并且该方法运行以请求滚动,UITextView也不会滚动,直到后台方法完成。
您不在后台执行。当你阻塞主线程时,UI只会冻结。 – jakenberg