我知道 UI 应该只在主线程上更新,但是是否可以在单独的线程上创建和添加子视图,只要它们没有添加到可见视图中?它会导致内存和性能问题吗?这是一些示例代码。
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
// do some fancy calculations, building views
UIView *aView = ..
for (int i, i<1000, i++)
{
UIView *subView = …
[aView addSubview:subView];
}
// Update UI on Main Thread
[queue addOperationWithBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Update the interface
[self.view addSubview:aView];
}];
}];
}];