NSFetchedResultsController que devuelve objetos con null indexPaths

Los detalles están en los comentarios.

El siguiente código:

// Perform the fetch...
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

// Confirm that objects were fetched by counting them...
NSLog(@"Number of Objects = %i",
      [[fetchedResultsController fetchedObjects] count]);

// Confirm that sections exist by counting them...
NSLog(@"Numbers of Sections = %i",
      [[fetchedResultsController sections] count]); 

for (id section in [fetchedResultsController sections]) {
    // Count number of objects in each section
    // _The fact that this outputs 0 is the first sign of trouble_
    NSLog(@"Number of Objects in Section = %i", [section numberOfObjects]);
}

for (Reminder *reminder in [fetchedResultsController fetchedObjects]) {
    // Confirm that the objects fetched are in fact real objects
    // by accessing their "textContent" property...
    NSLog(@"textContent=%@", reminder.textContent);

    // Show that the fetched objects are being returned 
    // with a (null) indexPath...
    // _The second sign of trouble..._
    NSLog(@"IndexPath=%@",
          [fetchedResultsController indexPathForObject:reminder]);
}

NSUInteger indexArr[] = {0,0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr 
                                                    length:2];

// _Application crashes on this line because the fetched 
// objects do not have indexPaths_
Reminder *testReminder = (Reminder *)[fetchedResultsController 
                                      objectAtIndexPath:indexPath]; 
NSLog(@"textContent = %@", testReminder.textContent);

Resultados en la siguiente salida:

2010-07-17 00:48:41.865 Reminders[27335:207] Number of Objects = 3
2010-07-17 00:48:41.867 Reminders[27335:207] Numbers of Sections = 1
2010-07-17 00:48:41.868 Reminders[27335:207] Number of Objects in Section = 0
2010-07-17 00:48:41.870 Reminders[27335:207] textContent=Imported Object 3
2010-07-17 00:48:41.871 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.873 Reminders[27335:207] textContent=Imported Object 2
2010-07-17 00:48:41.873 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.874 Reminders[27335:207] textContent=Imported Object 1
2010-07-17 00:48:41.875 Reminders[27335:207] IndexPath=(null)
2010-07-17 00:48:41.887 Reminders[27335:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

Cualquier idea sería muy apreciada. Para su información, el código anterior funciona perfectamente en una aplicación separada si uso una plantilla diferente como punto de partida. Es decir. si uso la plantilla "Aplicación basada en Windows", el código fallará. Si uso "Aplicación basada en navegación", el código funciona como se esperaba.

Actualizar: TechZen se preguntaba si el problema es causado por mi entidad Recordatorio. Pensé que era una buena idea, así que hice lo siguiente:

Cree dos aplicaciones de plantilla predeterminadas: una "aplicación basada en Windows" y una "aplicación basada en navegación" (ambas con Core Data habilitado)

Copiado sobre el código mínimo necesario desde el basado en Nav hasta el basado en Windows para realizar la prueba anterior (prácticamente solo el archivo "xcdatamodel", el controlador de resultados obtenidos y una forma de agregar objetos de prueba).

El código anterior todavía falla en la nueva aplicación basada en la ventana "Recordatorio libre de entidades". (En esta nueva aplicación de prueba, de hecho, hay un código cero que he creado yo mismo (fuera del código de prueba), todo es solo código de plantilla cortado y pegado).

Así que ahora estoy buscandode todas formas para ejecutar el código anterior después de crear una "aplicación basada en Windows". Aquí está el código para realizar la prueba utilizando la entidad predeterminada del navegador, en caso de que alguien esté interesado en probarlo:

ACTUALIZAR Tenga en cuenta que, como TechZen señaló a continuación, este código se bloqueará sin importar si se ejecuta con una base de datos vacía, por lo que si comienza desde una aplicación basada en ventanas, primero agregue algunos objetos a la base de datos y luego agregue el código de prueba.

// Confirm that objects were fetched
NSLog(@"Number of Objects = %i",
      [[fetchedResultsController fetchedObjects] count]);

// Confirm that sections exist
NSLog(@"Numbers of Sections = %i",
      [[fetchedResultsController sections] count]); 

for (id section in [fetchedResultsController sections]) {

    // Count number of objects in sections
    // _The fact that this outputs 0 is the first sign of trouble_
    NSLog(@"Number of Objects in Section = %i", [section numberOfObjects]);
}

for (NSManagedObject *managedObject in [fetchedResultsController fetchedObjects]) {

    // Confirm that the objects fetched are in fact real objects, 
    // by accessing their "timeStamp" property
    NSLog(@"TimeStamp=%@", [[managedObject valueForKey:@"timeStamp"] description]);

    // Show that the fetched objects are being returned 
    // with a (null) indexPath
    // _The second sign of trouble..._
    NSLog(@"IndexPath=%@",
          [fetchedResultsController indexPathForObject:managedObject]);
}

NSUInteger indexArr[] = {0,0};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexArr 
                                                    length:2];

// _Application crashes on this line, because the fetched 
// objects do not have indexPaths_
NSManagedObject *managedObject = [fetchedResultsController 
                                  objectAtIndexPath:indexPath];
NSLog(@"textContent = %@", [[managedObject valueForKey:@"timeStamp"] description]);

ACTUALIZAR aquí está la salida cuando se usa el nuevo código cortado y pegado

2010-07-18 15:33:41.264 Reminders[30898:207] Number of Objects = 3
2010-07-18 15:33:41.266 Reminders[30898:207] Numbers of Sections = 1
2010-07-18 15:33:41.267 Reminders[30898:207] Number of Objects in Section = 0
2010-07-18 15:33:41.270 Reminders[30898:207] TimeStamp=2010-07-18 13:59:00 -0400
2010-07-18 15:33:41.271 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.272 Reminders[30898:207] TimeStamp=2010-07-18 13:59:00 -0400
2010-07-18 15:33:41.273 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.274 Reminders[30898:207] TimeStamp=2010-07-18 13:58:59 -0400
2010-07-18 15:33:41.275 Reminders[30898:207] IndexPath=(null)
2010-07-18 15:33:41.276 Reminders[30898:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

ACTUALIZAR Así que reduje este problema a un problema relacionado con la versión del SDK, ahora tengo un proyecto que si compilo con Simulator 3.2 se bloquea, y compilar con Simulator 3.1.3 funciona bien. PERO si agrego un UITableViewController, luego compilo con Simulator 3.2, entonces funciona bien nuevamente. Entonces he creado unnuevo stackoverflow publique para hacer la pregunta: si está utilizando NSFetchedResultsController sin un UITableViewController, ¿cómo interactúa con los objetos? (ya que los IndexPaths no son confiables).

ACTUALIZAR Este problema se resuelve (tentativamente) mediante el uso de -[NSFetchedResultsController fetchedObjects] objectAtIndex:] para acceder a los objetos.

Respuestas a la pregunta(6)

Su respuesta a la pregunta