'NSGenericException',原因:集合 <__NSArrayM:0x7fabb400>在枚举时发生了变异 [英] &#39;NSGenericException&#39;, reason: Collection &lt;__NSArrayM: 0x7fabb400&gt; was mutated while being enumerated

查看:70
本文介绍了'NSGenericException',原因:集合 <__NSArrayM:0x7fabb400>在枚举时发生了变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iPhone 应用程序中,我正在尝试使用 uicollectionView 实现一个图像库.当 <20 个图像存储在库中时它工作正常,但是当我存储多个图像(大于 20 个)应用程序崩溃.请帮助我.谢谢提前.

In my iPhone app,i am trying to implement a image gallery using uicollectionView .when <20 images stores in gallery its working fine but when i am storing multiple image(greater than 20) app crashes.please help me.thanks in advance.

错误:

Terminating app due to uncaught exception 'NSGenericException', reason:          '*** Collection <__NSArrayM: 0x7fabb400> was mutated while being enumerated.'
*** First throw call stack:
(
    0   CoreFoundation                      0x042ea946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x03660a97 objc_exception_throw + 44
    2   CoreFoundation                      0x042ea1e6 __NSFastEnumerationMutationHandler + 166
    3   CoreFoundation                      0x041da052 -[NSArray containsObject:] + 178
    4   AtSceneDebug                        0x001ce9a9 -[GalleryCollectionViewController loadImageFromAppSupport:] + 201
    5   AtSceneDebug                        0x001c5de2 __73-[GalleryCollectionViewController collectionView:cellForItemAtIndexPath:]_block_invoke + 98
    6   libdispatch.dylib                   0x03cca30a _dispatch_call_block_and_release + 15
    7   libdispatch.dylib                   0x03ceae2f _dispatch_client_callout + 14
    8   libdispatch.dylib                   0x03cd310f _dispatch_root_queue_drain + 634
    9   libdispatch.dylib                   0x03cd487e _dispatch_worker_thread2 + 45
    10  libsystem_pthread.dylib             0x04056dab _pthread_wqthread + 336
    11  libsystem_pthread.dylib             0x0405acce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException

代码:

GalleryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GalleryCollectionViewCellID" forIndexPath:indexPath];

    if(indexPath.row==0)
    {
        Photo *photo=[self.photoSource.photos objectAtIndex:indexPath.row];
        cell.selectionImageView.hidden=YES;
        [cell.galleryImageView setImage:[UIImage imageNamed:[photo.mURLLarge lastPathComponent]]];
        return cell;
    }

    Photo *photo=[self.photoSource.photos objectAtIndex:indexPath.row];

    if(self.mItemType==ITEM_TYPE_PHOTO)

    {
        NSLog(@"index of coll.......%d",indexPath.row);
        UIImage *image = [_imageCache objectForKey:photo.mURLThumb];
      if(image)
        {
           //[cell.galleryImageView setImage:[self loadImageFromAppSupport:photo.mURLLarge]];
          [cell.galleryImageView setImage:image];

        }
      else
      {        
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{          
              UIImage *image = [self loadImageFromAppSupport:photo.mURLThumb];
              if(image)
              {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      GalleryCollectionViewCell *cell =(GalleryCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
                      if(cell)
                      {
                          cell.galleryImageView.image = image;
                      }
                  });

                  [_imageCache setObject:image forKey:photo.mURLThumb];
              }
          });

      }
    }

    else

    {
        [cell.galleryImageView setImage:[self loadImageFromAppSupport:photo.mURLThumb]];

    }

    if(indexPath.row==0)
    {
        cell.selectionImageView.hidden=YES;
    }
    else if(self.isEditing && photo.isMarkedForDeletion && indexPath.row!=0)
    {
        cell.selectionImageView.hidden=NO;
        [cell.selectionImageView setImage:[UIImage imageNamed:@"red_led"]];
    }
    else if (self.isEditing && !photo.isMarkedForDeletion && indexPath.row!=0)
    {
        //[cell.selectionImageView setImage:[UIImage imageNamed:@"green_led"]];
        cell.selectionImageView.hidden=YES;
    }
    else if(!self.isEditing && photo.isMarkedForDeletion && indexPath.row!=0)
    {
        cell.selectionImageView.hidden=YES;
    }
    else if(!self.isEditing && !photo.isMarkedForDeletion && indexPath.row!=0)
    {
        cell.selectionImageView.hidden=YES;
    }

    if (!self.isEditing)
    {
        CGFloat xx=cell.frame.origin.x;
        CGFloat yy=cell.frame.origin.y;

        CGRect frame = cell.frame;
        frame.origin.x = 500; // new x coordinate
        frame.origin.y = 0; // new y coordinate
        cell.frame = frame;

                [UIView animateWithDuration:0.0 animations:^{

            CGRect frame = cell.frame;
            frame.origin.x = xx; // new x coordinate
            frame.origin.y = yy; // new y coordinate
            cell.frame = frame;
        }];

    }
    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    return cell;

推荐答案

当您在 2 个不同的线程/队列上使用 NSMutable 时,通常会抛出此错误,一个正在枚举一个,而另一个正在向其中添加/删除元素.你永远不应该在多线程环境中使用 NSMutableObject.您的来电:

This error is often thrown when you use a NSMutable on 2 different thread/queue, and one is enumerating one while the other is adding / removing element from it. You should never use NSMutableObject in a multi threaded environment. Your call:

[_imageCache setObject:image forKey:photo.mURLThumb];

是失败的原因,您在主线程中访问它时正在更改调度队列中的可变对象.将此调用移动到if(cell)"部分(或 dispatch_get_main_queue 块中的任何其他位置)的正上方将解决它.

is the reason for the failure,you are changing a mutable object in a dispatch queue while accessing it in the main thread. Move this call just above the "if(cell)" part (or anywhere else in the dispatch_get_main_queue block) will solve it.

这篇关于'NSGenericException',原因:集合 <__NSArrayM:0x7fabb400>在枚举时发生了变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆