Question

I got the below error while fetching some data from sever. While fetching data, i m displaying an custom alert view with activity indicator. Alert is using main thread while data fetching is done by NSThread. This problem occurs only at some time, not always. I m not able to understand what to do.

* Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection was mutated while being enumerated.( "", "", "" )'

* Call stack at first throw: (

0   CoreFoundation                      0x00f2abe9 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x0107f5c2 objc_exception_throw + 47
2   CoreFoundation                      0x00f2a6a9 __NSFastEnumerationMutationHandler + 377
3   UIKit                               0x00371b8e -[UIView(Hierarchy) _findFirstSubviewWantingToBecomeFirstResponder] + 189
4   UIKit                               0x00366a13 -[UIView(Hierarchy) _promoteDescendantToFirstResponderIfNecessary] + 42
5   MyApplnName                        0x00015154 -[SecondViewController getDataFromServer] + 1798
6   Foundation                          0x000a1d4c -[NSThread main] + 81
7   Foundation                          0x000a1cd8 __NSThread__main__ + 1387
8   libSystem.B.dylib                   0x928db7fd _pthread_start + 345
9   libSystem.B.dylib                   0x928db682 thread_start + 34

)

terminate called after throwing an instance of 'NSException'

Was it helpful?

Solution

The problem is in part of your code where you enumerate some object, for example, NSMutableArray or NSMutableSet, using for-loop (for (id _obj in object)) and in that loop removing or adding some objects to the same object. It is not possible and causes exception.

You can replace that for-loop with for-loop using index variable:

 for (int i=0; i<[object count]; i++)
 {
    //add or remove objects if you want
 } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top