Remove imageView sublayer from TableViewCell

When I have an image I insert sublayer with CAGradientLayer,

...
layer.name = @"Gradient";
[cell.imageView.layer insertSublayer:layer atIndex:0];

and when there is no image for ImageView I need to remove this sublayer.
I tried different ways with no success. The last I tried was

  • CALayer optimizations?
  • CoreAnimation - Opacity Fade In and Out Animation Not Working
  • Core Data memory usage while importing large dataset
  • Clustering Google Maps Markers in iOS
  • MagicalRecord relationship mapping duplicates objects despite primary keys
  • Swift: Reflecting properties of subclass of NSManagedObject
  • for (CALayer *layer in cell.imageView.layer.sublayers) {
        if ([layer.name isEqualToString:@"Gradient"]) {
            [layer removeFromSuperlayer];
        }
    
    }
    

    but it gives me an error:

    CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  *** Collection <CALayerArray: 0xc1502e0> was mutated while being enumerated. with userInfo (null)
    

    Solutions Collect From Internet About “Remove imageView sublayer from TableViewCell”

    The exception is thrown because you are changing the contents of the sublayers array while enumerating it with foreach loop. This is not something special to layers, a similar exception is thrown when you add/remove objects while enumerating any NSMutableArray.

    You have various options to solve this issue

    Solution 1: Stop enumerating as soon as you modify the array.

    for (CALayer *layer in cell.imageView.layer.sublayers) {
        if ([layer.name isEqualToString:@"Gradient"]) {
            [layer removeFromSuperlayer];
            break;
        }
    }
    

    Solution 2: Do not enumerate the real array, instead use its copy.

    NSArray* sublayers = [NSArray arrayWithArray:cell.imageView.layer.sublayers];
    for (CALayer *layer in sublayers) {
        if ([layer.name isEqualToString:@"Gradient"]) {
            [layer removeFromSuperlayer];
        }
    }
    

    Solution 3: Use key value coding to keep a reference to the gradient layer.

    Set it after inserting:

    [cell.imageView.layer insertSublayer:layer atIndex:0];
    [cell.imageView.layer setValue:layer forKey:@"GradientLayer"];
    

    Retrieve and remove it

    CALayer* layer = [cell.imageView.layer valueForKey:@"GradientLayer"];
    [layer removeFromSuperlayer];
    [cell.imageView.layer setValue:nil forKey:@"GradientLayer"];