App crashes with reason: Collection <__NSArrayM: 0x7071700> was mutated while being enumerated

前端 未结 3 635
离开以前
离开以前 2021-01-24 13:04

Can you please help me to find reason why my app crashes after clicking on segmented control? Not allways, but usualy after some clicks.

Error message:

         


        
相关标签:
3条回答
  • 2021-01-24 13:39

    I think the problem is in these line of code

    for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]) 
    {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
    

    and you are using it in two methods didLoad and didUnload. The reason that you are getting because you are updating the content of the following array

    [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]
    

    When you go thorough enumerator class you will get this caution of "not updating the content while enumerator is in use" ... So to overcome this problem simply do following to all the places where you have used this code....

    NSArray* temp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
    for (NSString *file in temp) 
    {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
    }
    

    i hope this would solve your problem...

    0 讨论(0)
  • 2021-01-24 13:58

    Looks like the problem was with UIImagePickerController and only in simulator (more described in details here: http://blog.airsource.co.uk/index.php/2008/11/12/memory-usage-in-uiimagepickercontroller/ ). On real device everything works quite fine.

    0 讨论(0)
  • 2021-01-24 13:58

    solved

    I had same problem in coredata objects enumeration.

    Do not change sub object which relies in ARRAY on which Loop is Running

    Like ==>>

    If I change/Modify object while in loop, it will give this error.

     for (LoadList *objLL in ArrLoadList) {             // here Enumaration is Going on
            // here I removed objects in ArrLoadList and reassign the ArrLoadList .. 
            // it will give me this error 
            // So Don't change the Main Array object 
     }
    
    0 讨论(0)
提交回复
热议问题