collection was mutated when enumerated." The goal is to "start the graphical counter when starting viewWillA...">

Crash - "The <CALayerArray: 0x645dfc0> collection was mutated when enumerated."

The goal is to "start the graphical counter when starting viewWillAppear, which loads the data before showing the table view", so the user does not wonder why there is a delay before viewing the table. That is, a UIActivityIndicatorView was added to the window, and I just want to set alpha to hide / show it.

I get this strange error when starting a thread to make sure that the image of "spinning gears" (tag = 333) is displayed before proceeding with loading / calculating material in viewWillAppear.

I do not get it every time I call [appdel addGearz] and [appdel removeGearz], this happens both for both random and random. This can happen after 2 viewWillAppears or after 15. If I comment on the line that sets the alpha, everything will work.

A typical viewWillAppear looks something like this:

[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 

And here is the code that crashes if the lines with .alpha are not commented out

 -(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]; } 

I used another user's code, so ... something obvious that you can see? Of course, should I be able to change the alpha of the UIViews in the stream? Do I need to "embed" an alpha change in some "enumerate stop while I change this" code?

I did not crash by moving this alpha line one level above the pool or lower [pool drain], but then I get a lot of "auto-implemented without a pool in place - just leaks" mass.

Apparently, I don’t understand something about this stream code.

+7
source share
1 answer

You should not try to modify the user interface in a separate thread. The user interface should only be managed in the main thread.

Instead of detaching a new thread, you should use performSelectorOnMainThread:withObject:waitUntilDone: This ensures that the method is called into the appropriate thread.

+8
source

All Articles