美文网首页
__NSFastEnumerationMutationHandl

__NSFastEnumerationMutationHandl

作者: 统领三界 | 来源:发表于2018-08-20 12:28 被阅读60次

    xcode 获取的崩溃日志中有一篇日志信息如下


    image.png

    查看代码videoDrawViewWillHide方法中时一个数组遍历的方法,查询相关资料后差不多可以确定是在遍历数组的过程中修改或者删除了数组的元素导致, 编写demo 重现问题

      removeArr = [[NSMutableArray alloc] initWithCapacity:1];
        
        for (int i = 0; i < 100; i++) {
            [removeArr addObject:[NSString stringWithFormat:@"%d", I]];
        }
        
        dispatch_async(dispatch_queue_create("ergodicArr", DISPATCH_QUEUE_SERIAL), ^{
            [self ergodicArr];
        });
        
        dispatch_async(dispatch_queue_create("removaArr", DISPATCH_QUEUE_SERIAL), ^{
            [self removeArry];
        });
    
    -(void)removeArry{
        for (int i = 20; i < 30; i++) {
            [self->removeArr removeObject:[NSString stringWithFormat:@"%d",I]];
        }
        NSLog(@"removearr = %@", self->removeArr);
    }
    
    -(void)ergodicArr
    {
        for (NSString *obj in removeArr) {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"obj = %@", obj);
        }
    }
    

    编译运行结果如下


    image.png

    得到了与崩溃日志中相同的结果
    解决方案:修改遍历方法

     dispatch_async(dispatch_queue_create("ergodicArr", DISPATCH_QUEUE_SERIAL), ^{
            [self ergodicArr1];
        });
    
    -(void)ergodicArr1
    {
        [removeArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"obj = %@", obj);
        }];
    }
    

    编译运行 正常

    相关文章

      网友评论

          本文标题:__NSFastEnumerationMutationHandl

          本文链接:https://www.haomeiwen.com/subject/ldwkiftx.html