Block_release在后台线程上取消分配UI对象

| 在WWDC 2010“区块和中央中央调度”演讲中介绍的模式之一是使用嵌套的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];
    });
});
由于在块内使用了\“ myViewController \”,因此它会自动获得一个\'retain \',并在以后清理该块时得到一个\'release \'。 如果该块的\'release \'调用是最终的释放调用(例如,在后台任务运行时用户从视图中移开),则调用了myViewController dealloc方法,但是该方法被调用了后台线程!! UIKit对象不喜欢在主线程之外进行取消分配。就我而言,UIWebView引发异常。 WWDC所呈现的模式(如何被称为避免UI锁定的最佳新方法)如何如此有缺陷?我想念什么吗?     
已邀请:
        在这种情况下,可以使用“ 1”存储类型限定符。 block1ѭ变量不会被块自动保留。因此,您需要自己保留对象:
__block UIViewController *viewController = [myViewController retain];
dispatch_async(backgroundQueue, ^{
    // Do long-running work here.
    dispatch_async(dispatch_get_main_queue(), ^{
        [viewController updateUIWithResults:results];
        [viewController release]; // Ensure it\'s released on main thread
    }
});
编辑 使用ARC,__ block变量对象会自动被该块保留,但是我们可以将__block变量的值设置为nil,以便在需要时释放保留的对象。
__block UIViewController *viewController = myViewController;
dispatch_async(backgroundQueue, ^{
    // Do long-running work here.
    dispatch_async(dispatch_get_main_queue(), ^{
        [viewController updateUIWithResults:results];
        viewController = nil; // Ensure it\'s released on main thread
    }
});
    
        在线程中,我只使用
[viewController retain];
,然后在线程结束时使用
[viewController release]
。它有效,我不使用
GCD~
    
        这对我有用(添加了一个计时器):
[self retain]; // this guarantees that the last release will be on the main threaad
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];
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(releaseMe:) userInfo:nil repeats:NO];
    });
});
- (void)releaseMe:(NSTimer *)theTimer {
    [self release]; // will be on the main thread
}
    

要回复问题请先登录注册