strange memory error when deleting object from Core Data

Posted by llloydxmas on Stack Overflow See other posts from Stack Overflow or by llloydxmas
Published on 2010-06-12T06:08:39Z Indexed on 2010/06/12 6:12 UTC
Read the original article Hit count: 251

Filed under:
|
|

I have an application that downloads an xml file, parses the file, and creates core data objects while doing so. In the parse code I have a function called 'emptydatacontext' that removes all items from Core Data before creating replacements from the xml file. This method looks like this:

-(void) emptyDataContext
{
 NSMutableArray* mutableFetchResults = [CoreDataHelper getObjectsFromContext:@"Condition" :@"road" :NO :managedObjectContext];
 NSFetchRequest * allCon = [[NSFetchRequest alloc] init];
 [allCon setEntity:[NSEntityDescription entityForName:@"Condition" inManagedObjectContext:managedObjectContext]];
 NSError * error = nil;
 NSArray * conditions = [managedObjectContext executeFetchRequest:allCon error:&error];
 [allCon release];

 for (NSManagedObject * condition in conditions) {
  [managedObjectContext deleteObject:condition];
 }
}

The first time this runs it deletes all objects and functions as it should - creating new objects from the xml file. I created a 'update' button that starts the exact same process of retrieving the file the preceeding as it did the first time. All is well until its time to delete the core data objects again. This 'deleteObject' call creates a "EXC_BAD_ACCESS" error each time. This only happens on the second time through.

See this image for the debugger window as it appears when walking through the deletion FOR loop on the second iteration. Conditions is the fetched array of 7 objects with the objects below. Condition should be an individual condition.

link text

As you can see 'condition' does not match any of the objects in the 'conditions' array. I'm sure this is why I'm getting the memory access errors. Just not sure why this fetch (or the FOR) is returning a wrong reference. All the code that successfully performes this function on the first iteration is used in the second but with very different results.

Thanks in advance for the help!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c