Ios 即使锁定后线程崩溃<__NSArrayM:0x7f881a6b1900>;在被枚举时是否发生了变异?
这是我的代码,我正在删除我的条件下的多个值Ios 即使锁定后线程崩溃<__NSArrayM:0x7f881a6b1900>;在被枚举时是否发生了变异?,ios,objective-c,thread-safety,nsmutablearray,nslock,Ios,Objective C,Thread Safety,Nsmutablearray,Nslock,这是我的代码,我正在删除我的条件下的多个值 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { PhotoCan *cell=[collectionView cellForItemAtIndexPath:indexPath]; UIImage *getimg=[dicPhotoStore objectForKey:@
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCan *cell=[collectionView cellForItemAtIndexPath:indexPath];
UIImage *getimg=[dicPhotoStore objectForKey:@(indexPath.row)];
BOOL integer=[dic objectForKey:@(indexPath.row)];
if(integer)
{
for(id img in arrFinalStore)
{
if(img==getimg)
{
NSLock *arrayLock = [[NSLock alloc] init];
[arrayLock lock];
[arrFinalStore removeObject:img];
NSLog(@"crash inside");
[arrayLock unlock];
}
}
@synchronized(self)
{
[dic removeObjectForKey:@(indexPath.row)];
[dicPhotoStore removeObjectForKey:@(indexPath.row)];
}
NSLog(@"crashoutside");
NSLog(@"inside false");
}
else
{
[dicPhotoStore setObject:cell.imgView.image forKey:@(indexPath.row)];
[arrFinalStore addObject:cell.imgView.image];
[dic setObject:@"1" forKey:@(indexPath.row)];
}
[_collection reloadData];
}
我理解在试图找出这个错误时出现了一些错误,请帮助我克服这个错误
这是一个错误:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x7f881a6b1900> was mutated while being enumerated.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010299fe65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000102418deb objc_exception_throw + 48
2 CoreFoundation 0x000000010299f7c4 __NSFastEnumerationMutationHandler + 132
3 PhotoCollageCanvas 0x000000010056878f -[photoAssets collectionView:didSelectItemAtIndexPath:] + 767
4 UIKit 0x0000000103afb4a7 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 701
5 UIKit 0x0000000103b1d049 -[UICollectionView touchesEnded:withEvent:] + 574
6 UIKit 0x00000001034b6ef7 forwardTouchMethod + 349
7 UIKit 0x00000001034b6fc0 -[UIResponder touchesEnded:withEvent:] + 49
8 UIKit 0x00000001034b6ef7 forwardTouchMethod + 349
9 UIKit 0x00000001034b6fc0 -[UIResponder touchesEnded:withEvent:] + 49
10 UIKit 0x0000000103783ede _UIGestureRecognizerUpdate + 10279
11 UIKit 0x0000000103319f8a -[UIWindow _sendGesturesForEvent:] + 1137
12 UIKit 0x000000010331b1c0 -[UIWindow sendEvent:] + 849
13 UIKit 0x00000001032c9b66 -[UIApplication sendEvent:] + 263
14 UIKit 0x00000001032a3d97 _UIApplicationHandleEventQueue + 6844
15 CoreFoundation 0x00000001028cba31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x00000001028c195c __CFRunLoopDoSources0 + 556
17 CoreFoundation 0x00000001028c0e13 __CFRunLoopRun + 867
18 CoreFoundation 0x00000001028c0828 CFRunLoopRunSpecific + 488
19 GraphicsServices 0x00000001058d8ad2 GSEventRunModal + 161
20 UIKit 0x00000001032a9610 UIApplicationMain + 171
21 Photo 0x0000000100566a5f main + 111
22 libdyld.dylib 0x000000010697b92d start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
***由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“***集合在枚举时发生了变异。”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x000000010299fe65例外预处理+165
1 libobjc.A.dylib 0x0000000102418deb objc_异常_抛出+48
2 CoreFoundation 0x000000010299f7c4\uu\NSFastEnumerationMutationHandler+132
3 PhotocollectionCanvas 0x000000010056878f-[photoAssets collectionView:didSelectItemAtIndexPath:+767
4 UIKit 0x0000000103afb4a7-[UICollectionView\u SelectItemAtIndex路径:动画:滚动位置:notifyDelegate:+701
5 UIKit 0x0000000103b1d049-[UICollectionView触控:withEvent:][574
6 UIKit 0x00000001034b6ef7 forwardTouchMethod+349
7 UIKit 0x00000001034b6fc0-[UIResponder touchesEnded:withEvent:+49
8 UIKit 0x00000001034b6ef7 forwardTouchMethod+349
9 UIKit 0x00000001034b6fc0-[UIResponder touchesEnded:withEvent:+49
10 UIKit 0x0000000103783ede消化识别器更新+10279
11 UIKit 0x0000000103319f8a-[UIWindow\u sendGesturesForEvent:+1137
12 UIKit 0x000000010331b1c0-[UIWindow sendEvent:+849
13 UIKit 0x00000001032c9b66-[UIApplication sendEvent:+263
14 UIKit 0x00000001032a3d97 _UIApplicationHandleEventQueue+6844
15 CoreFoundation 0x00000001028cba31\uuuu CFRUNLOOP\u正在调用\u OUT\u以执行\u函数\uuu+17
16 CoreFoundation 0x00000001028c195c\uuu CFRunLoopDoSources0+556
17 CoreFoundation 0x00000001028c0e13\uuu CFRunLoopRun+867
18 CoreFoundation 0x00000001028c0828 CFRunLoopRunSpecific+488
19图形服务0x00000001058d8ad2 GSEventRunModal+161
20 UIKit 0x00000001032a9610 UIApplicationMain+171
21照片0x0000000100566a5f干管+111
22 libdyld.dylib 0x000000010697b92d开始+1
23???0x0000000000000001 0x0+1
)
在遍历数组时不能删除元素
[arrFinalStore removeObject:img];
只需使用
[arrFinalStore removeObject:img]
在遍历数组时不能删除元素
[arrFinalStore removeObject:img];
只需使用
[arrFinalStore removeObject:img]
你认为那把锁能做什么?它完全不起作用。你创建了一个锁,锁定了它,解锁了它。由于该代码是唯一可以访问锁的代码,因此该锁不可能阻止任何人执行任何操作
无论如何,这也解决不了你的问题。有人正在枚举该数组。在枚举它时,在您尝试修改它的任何尝试都将崩溃
您的代码是(删除了行)
这段代码绝对会崩溃,没有什么可以阻止它崩溃。迭代时不能从数组中删除对象 你认为那把锁能做什么?它完全不起作用。你创建了一个锁,锁定了它,解锁了它。由于该代码是唯一可以访问锁的代码,因此该锁不可能阻止任何人执行任何操作 无论如何,这也解决不了你的问题。有人正在枚举该数组。在枚举它时,在您尝试修改它的任何尝试都将崩溃 您的代码是(删除了行)
这段代码绝对会崩溃,没有什么可以阻止它崩溃。迭代时不能从数组中删除对象 这不是锁的问题。但问题是,在迭代时,您试图删除元素,这意味着您正在进行并发修改,这会引发异常 下面是一个您可以使用的示例
for (item in originalArrayOfItems) {
if ([item shouldBeDiscarded])
[discardedItems addObject:item];}
[originalArrayOfItems removeObjectsInArray:DiscardedEditEMS] 这不是锁的问题。但问题是,在迭代时,您试图删除元素,这意味着您正在进行并发修改,这会引发异常 下面是一个您可以使用的示例
for (item in originalArrayOfItems) {
if ([item shouldBeDiscarded])
[discardedItems addObject:item];}
[originalArrayOfItems removeObjectsInArray:DiscardedEditEMS] 在遍历数组时不能删除元素,所以可以使用所有不必要的对象创建临时数组,然后调用
-(void)removeObjectsInArray:(NSArray*)otherArray
因此,您的代码是:
NSMutableArray *tempArray = [NSMutableArray array];
for(id img in arrFinalStore)
{
if(img==getimg)
{
[tempArray addObject:img];
}
}
[arrFinalStore removeObjectsInArray:tempArray];
您还可以使用simple
for(int i=arrfinalstoer.count-1;i>0;--i)
进行枚举。在遍历数组时无法删除元素,因此您可以使用所有不必要的对象创建临时数组,然后调用-(void)removeObjectsInArray:(NSArray*)otherArray
因此,您的代码是:
NSMutableArray *tempArray = [NSMutableArray array];
for(id img in arrFinalStore)
{
if(img==getimg)
{
[tempArray addObject:img];
}
}
[arrFinalStore removeObjectsInArray:tempArray];
您还可以使用simple
for(int i=arrfinalstoer.count-1;i>0;--i)
进行枚举。您不能[arrFinalStore removeObject:img]代码>而是创建新数组并编辑您不能[arrFinalStore removeObject:img]代码>改为创建新数组和edit@gnasher728兄弟我搜索了崩溃原因并读取了ii将由于线程而发生,为了停止线程,我发布了此代码以便使用,所以你说我错了如何克服此问题。@gnasher728兄弟我搜索了崩溃原因并读取了ii将