Kev*_*ner 4 objective-c thread-safety dealloc ios objective-c-blocks
这是我的情况.这很复杂所以请耐心等待.
我有一个视图类,让我们称之为MyView.它会创建一个加载指示器子视图,然后启动一个将加载数据的后台操作.它还会创建一个块,后台队列操作在完成后将在主队列中排队.该块通过添加UITextView带有加载数据的另一个子视图a来准备视图.当然,要做到这一点,块必须具有对视图的引用.
因此后台操作保留了块,块保留了视图.和我一起到目前为止?
有时,MyView在后台队列操作完成之前,会从其超级视图中删除实例.有时,在后台队列操作被完全清除之前,主要的队列操作(调用块)会被彻底清除.在这种情况下,MyView可以-dealloc在后台线程上调用它的实例,因为对视图的最后一个引用属于该块,并且对该块的最后一个引用属于后台操作.
UIKit不喜欢从任何线程调用,而是主线程.在这种情况下UITextView,显然甚至包括-dealloc电话.我在文本视图中得到EXC_BAD_ACCESS了一个叫做"web thread lock"的东西-dealloc.
我认为后台线程有时会有最后一个引用是合理的,我想在我的-dealloc实现中处理这个问题,如下所示:
- (void)dealloc {
if ([NSOperationQueue currentQueue] == [NSOperationQueue mainQueue]) {
// The usual -- dealloc subviews safely on the main thread
self.myIvar = nil;
[super dealloc];
}
else {
// Not on the main thread, so keep the object alive
// in spite of the dealloc call.
[self retain]; // explicit retain
[[NSOperationQueue mainQueue]
addOperationWithBlock:^{ // implicit retain at block creation
[self release]; // explicit release
}]; // implicit release, dealloc called again, but on the main thread
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当您调用-release一个对象时,如果保留计数达到零,则NSObject调用实现-dealloc.这一切都发生了吗?换句话说,是否可以接听电话-dealloc而不是来电super?我制作某种可恶的僵尸物品还是这样好吗?
如果这不行,那么确保-dealloc在主线程上调用的好方法是什么?
- (void)release
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
} else {
[super release];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是预ARC.不要用ARC做.
| 归档时间: |
|
| 查看次数: |
1475 次 |
| 最近记录: |