iphone - 崩溃 - "Collection <CALayerArray: 0x645dfc0> was mutated while being enumerated."

标签 iphone calayer enumeration nsthread nsautoreleasepool

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

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

我并没有在每次调用 [appdel addGearz] 和 [appdel removeGearz] 时得到它,这两者都会发生,而且是随机的。它可能会在 2 个 viewWillAppears 之后发生,或者在 15 个 viewWillAppears 之后发生。如果我注释掉设置 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 更改线移动到池分配上方或 [池排水] 下方,使其不会崩溃,但随后我收到很多“自动释放,没有池 - 只是泄漏”消息。

显然,我对这个线程代码有些不明白。

最佳答案

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

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

关于iphone - 崩溃 - "Collection <CALayerArray: 0x645dfc0> was mutated while being enumerated.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4757515/

相关文章:

android - PhoneGap互操作

c++ - 在开关情况下使用按位或运算符

python - 遍历列表的python列表并创建一个新列表

c++ - 在 C++ 中枚举 k 维超立方体顶点的最有效方法是什么?

iphone - iOS升级后会发生什么?

iphone - 如何在Three20的AppDelegate中添加全局右栏按钮(UINavigationController)

ios - CALayer 的实心边框

iphone - self.frame 与 self.layer.bounds(在 iOS 中)

ios - 捕获 UIView 的图像(renderInContext 无法正常工作)

iphone - 我怎样才能得到iPhone中两个时间之间的差异?