Linus
Linus

Reputation: 2819

iOS threading "Modifying layer that is being finalized"

I'm analyzing an image which takes some time and meanwhile I want to display a progress indicator. For this I'm using MBProgressHUD.

It almost works... I get this error: "Modifying layer that is being finalized". I guess it's due to the fact that I do pushViewController not in my main thread. Am I right? Any ideas on how to correct this issue?

My code:

- (IBAction)buttonReadSudoku:(id)sender
{    
    mbProgress=[[MBProgressHUD alloc] initWithView:self.view];
    mbProgress.labelText=@"Läser Sudoku";
    [self.view addSubview:mbProgress];
    [mbProgress setDelegate:self];

    [mbProgress showWhileExecuting:@selector(readSudoku) onTarget:self withObject:nil animated:YES];
}

- (void)readSudoku
{
    UIImage *image = imageView.image;
    image = [ImageHelpers scaleAndRotateImage:image];
    NSMutableArray *numbers = [SudokuHelpers ReadSudokuFromImage:image];

    sudokuDetailViewController = [[SudokuDetailViewController alloc] init];
    [sudokuDetailViewController setNumbers:numbers];

    [[self navigationController] pushViewController:sudokuDetailViewController animated:YES];
}

Upvotes: 0

Views: 2015

Answers (2)

Matthew Frederick
Matthew Frederick

Reputation: 22305

All UI changes must be in the main thread, as you note. Rather than you off-main-thread method make any changes to the UI, send an NSNotification to the current viewController telling it to do the UI work.

This is an especially good route if you're crossing an MVC border, or if you already have a viewController that knows what to do so that writing a separate method results in duplicate code.

Upvotes: 0

Jonah
Jonah

Reputation: 17958

Define a new method to push your detail view controller and use -performSelectorOnMainThread:withObject:waitUntilDone: to perform it on the main thread. Don't try to make any UI changes from other threads.

Upvotes: 2

Related Questions