0

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'

JiteshW
  • 2,195
  • 4
  • 32
  • 61
  • I ain't a objective-C expert, but from looking at the error message I wana ask: In your code are you doing something like `foreach item in collection` and at the same time modify the collection in the loop body? – Ankur Oct 24 '11 at 06:05

1 Answers1

2

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
 } 
Nekto
  • 17,837
  • 1
  • 55
  • 65
  • No, I m not removing any object from for loop. What i actually do is , 1. I creates one object in for loop 2. If its is not Nil then adding it to one dictionary and then adding that object to an array. – JiteshW Oct 24 '11 at 06:20
  • Do you read my post? > that loop removing or **adding** some objects – Nekto Oct 24 '11 at 06:21
  • Sorry for above post. I figured it out, I changed my code and its working now. – JiteshW Oct 24 '11 at 06:31