views:

44

answers:

1

Hi,

I have a core-data object "entity" with 2 fields - "name" (a NSString *) and "type" (a EntityType). EntityType is another core data obect with 1 field - name (NSString *).
I am creating and deleting an entity object using the code similar in functionality to the following code -

Entity *e1 = [NSEntityDescription insertNewObjectForEntityName:"@Entity" inManagedObjectContext: context]

EntityType *type = [NSEntityDescription insertNewObjectForEntityName:"@EntityType" inManagedObjectContext: context]

e1.type = type;

Now I delete the entity object without assigning any values or saving to the persistent store.

if([e1.name length] == 0) {

  [context deleteObject:e1];

  NSError *nil;
  if(![context:save &error] {
    // log error
  }
}

At this point I see a crash, when I try to delete and save the object. (Terminating due to uncaught exception 'NSRangeException' reason: [NSCFArray removeObjectAtIndex]: index(0) beyond bounds (0)]

Any pointers/suggestions are much appreciated.

Here's the stack trace that was requested -

#0  0x3266bdf4 in objc_exception_throw ()
#1  0x32d73b32 in +[NSException raise:format:arguments:] ()
#2  0x32d73ad2 in +[NSException raise:format:] ()
#3  0x33f4a710 in _NSArrayRaiseBoundException ()
#4  0x33f7ba3c in -[NSCFArray removeObjectAtIndex:] ()
#5  0x30537dc6 in -[NSFetchedResultsController(PrivateMethods) _removeObjectInFetchedObjectsAtIndex:] ()
#6  0x3053b50a in -[NSFetchedResultsController(PrivateMethods) _postprocessDeletedObjects:] ()
#7  0x3053aa9a in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] ()
#8  0x33f765d8 in _nsnote_callback ()
#9  0x32d9e510 in _CFXNotificationPostNotification ()
#10 0x33f741b2 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#11 0x304b4388 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] ()
#12 0x3050768a in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] ()
#13 0x3049c2b0 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] ()
#14 0x304cb186 in -[NSManagedObjectContext save:] ()
#15 0x00006d5a in -[MyAppViewController goToMain] (self=0x11b730, _cmd=0x174b3) at /Users/Me/Projects/MyApp/Classes/MyAppViewController.m:611



2010-09-15 17:37:10.053 MyApp[1273:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray removeObjectAtIndex:]: index (0) beyond bounds (0)'
2010-09-15 17:37:10.066 MApp[1273:207] Stack: (
    853417245,
    845594132,
    852966195,
    852966099,
    ............
     .............
A: 

The error is coming from the fetchedResults array of the FRC. I suspect this is caused by you not implementing the FRC delegate methods properly such that when the tableview tries to update, it tries to access the zero index of an empty array.

When you have an FRC attached to any context, the FRC will update itself automatically when you make any change to context such as deleting an object (that is what the notifications in the trace stack are doing, telling the FRC to update.)

TechZen