Hi in one of my application. I am using queues to download a file from server and after file download completes I am updating the status in coredata (Sync type).While Update status in core data db app is crashing continously.
Here is the code which I used in my app
**In Download file method**
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadfileinThread:) object:params] ;
[operation addObserver:self forKeyPath:@"isFinished" options:0 context:nil];
[downloadQueue addOperation:operation] ;
**In Download file in Thread. (Here actual file will download)**
-(void) downloadfileinThread:
{
[self UpdateDatabase:file with:updatesArray1] ; //updatesArray1 contains dictionaries (Syncstatus:0 like this)
}
**DB updation**
-(void) UpdateDatabase:(id)_object with:(NSMutableArray *)updatesArray
{
NSManagedObjectContext *threadManagedObjectContext = [self myManagedContext] ;
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextObjectsDidChangeNotification object:threadManagedObjectContext] ;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeContextChangesForNotification:) name:NSManagedObjectContextObjectsDidChangeNotification object:threadManagedObjectContext];
NSManagedObject *object = [threadManagedObjectContext objectWithID:[_object objectID]] ;
if (updatesArray)
{
for (NSDictionary *updatedDic in updatesArray)
{
[object setValue:[[updatedDic allValues]lastObject] forKey:[[keyValue allKeys]lastObject]];
}
NSError *error;
bool result = [threadManagedObjectContext save:&error];
if (!result)
{
NSLog(@" error saving context, %@, %@", error, error.userInfo);
}
}
}
Crash Message: Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection <__NSDictionaryM: 0xd01b9e0> was mutated while being enumerated.'
Please help me in resolving this issue.