Search code examples
multithreadingiosmbprogresshud

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

Solution

  • 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.