崩溃 - “收集<calayerarray:0x645dfc0 =”“>在被枚举时发生了变异。”</calayerarray:>

时间:2011-01-21 10:08:31

标签: iphone calayer enumeration nsthread nsautoreleasepool

目标是“在viewWillAppear开始时启动一个微调器图形,在显示tableview之前加载数据”,这样用户就不会想知道为什么在看到表之前会有延迟。即窗口中添加了UIActivityIndi​​catorView,我只想将alpha设置为隐藏/显示它。

启动线程时出现这个奇怪的错误,以确保显示“旋转齿轮”imageview(tag = 333),然后再继续加载/计算viewWillAppear中的内容。

我在每次调用[appdel addGearz]和[appdel removeGearz]时都没有得到它,这两个都发生了,而且它是随机的。它可能发生在2个viewWillAppears之后,或15之后。如果我注释掉设置alpha的行,一切正常。

典型的viewWillAppear看起来像这样,

[super viewWillappear];
self.title=@"Products listing"; //and other simple things
[appdel addGearz];
[self getProducts];
[self getThumbnails];
[myTableView reloadData]; //in case view already loaded and coming back from subview and data changed

如果带有.alpha的行没有被注释掉,那么代码就会崩溃

-(void)addGearz {
    [NSThread detachNewThreadSelector:@selector(gearzOn) toTarget:self withObject:nil];
}

-(void)removeGearz {
    [NSThread detachNewThreadSelector:@selector(gearzOff) toTarget:self withObject:nil];
}

- (void)gearzOn {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [window viewWithTag:333].alpha=1.0;
        //
        //  [[window viewWithTag:333] setNeedsDisplay];
    [pool drain];
}

- (void) gearzOff {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [window viewWithTag:333].alpha=0.0;
        //
        //  [[window viewWithTag:333] setNeedsDisplay];
    [pool drain];
}

我使用过其他人的代码,所以......你能看到什么明显的东西?当然我必须能够在线程中更改UIViews的alpha吗?我是否需要在某些“停止枚举时我将其更改为”代码中“嵌入”alpha变量?

我通过将alpha-change-line移动到池alloc之上或者[pool drain]之下来使它没有崩溃,但后来我得到了很多“自动释放没有池到位 - 只是泄漏” - 消息。

显然,我对这个线程代码有些不了解。

1 个答案:

答案 0 :(得分:8)

您不得尝试在单独的线程上修改UI。 UI只应在主线程上进行操作。

您应该使用performSelectorOnMainThread:withObject:waitUntilDone:而不是分离新主题。这将确保在适当的线程上调用该方法。