小编Ral*_*ski的帖子

Block_release在后台线程上释放UI对象

WWDC 2010"Blocks and Grand Central Dispatch"演讲中提出的模式之一是使用嵌套的dispatch_async调用在后台线程上执行耗时的任务,然后在任务完成后更新主线程上的UI

dispatch_async(backgroundQueue, ^{
    // do something time consuming in background
    NSArray *results = ComputeBigKnarlyThingThatWouldBlockForAWhile();

    // use results on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [myViewController UpdateUiWithResults:results];
    });
});
Run Code Online (Sandbox Code Playgroud)

由于"myViewController"正在块中使用,它会自动获得"保留",并在清除块时稍后获得"释放".

如果块的'release'调用是最终的释放调用(例如,用户在后台任务运行时导航远离视图),则调用myViewController dealloc方法 - 但是它在后台线程上调用!!

UIKit对象不喜欢在主线程之外取消分配.在我的例子中,UIWebView抛出异常.

这个WWDC如何呈现模式 - 特别提到避免UI锁定的最佳新方法 - 是如此有缺陷?我错过了什么吗?

grand-central-dispatch ios objective-c-blocks

17
推荐指数
1
解决办法
4217
查看次数