views:

299

answers:

3

Details are in the comments.

The following code:

// 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);

Results in the following output:

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)'

Any ideas would be greatly appreciated. FYI, the above code works perfectly in a separate application if I use a different template as a starting point. I.e. if I use the "Window-based application" template, the code will fail. If I use "Navigation-based application" the code works as expected.

Thanks!

Update: TechZen was wondering if the problem is caused by my Reminder entity. I thought this was a good idea to look into, so I did the following:

  1. Create two default template applications: a "Window-based application" and a "Navigation-based Application" (both with Core Data enabled)

  2. Copied over the minimum code needed from the Nav-based to the Window-based to perform the above test (pretty much just the "xcdatamodel" file, the fetchedresultscontroller, and a way to add test objects).

The above code still fails in the new "Reminder-entity free" window-based application. (In this new test application there is in fact zero code I've authored myself (outside of the test code), it's all just template code cut-and-pasted together.)

So now, I am looking for any way to get the above code to run after creating a "Window-based application". Here is the code to perform the test using the nav-based's default entity, in case anyone is interested in giving it a try:

UPDATE Note that as TechZen noted below this code will crash no matter what if run with an empty database, so if starting from a window-based application, first add a few objects to the database then add the test code.

// 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]);

UPDATE here is the output when using the new cut-and-pasted code

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)'

UPDATE So I narrowed this problem down to being an SDK version-related issue, I now have a project that if I build with Simulator 3.2 it crashes, and build with Simulator 3.1.3 it works fine. BUT if I add a UITableViewController, then build with Simulator 3.2, then it works fine again. So I've created a new stackoverflow post to ask the question: if you are using NSFetchedResultsController without a UITableViewController, how do you interact with the objects? (since the IndexPaths are unreliable).

UPDATE This issue is (tentatively) solved by using -[NSFetchedResultsController fetchedObjects] objectAtIndex:] to access the objects.

A: 

What does the creation of the NSFetchedResultsController look like?

Update

FYI, you can update your question with the code instead of using PasteBin.

Are you using multiple threads in this code anywhere to access the NSFetchedResultsController?

Marcus S. Zarra
Boilerplate core data template, with only the two project-specific parts changed: 1. entityForName:@"Reminder" 2. initWithKey:@"textContent"Here it is in a pastebin incase there is anything helpful there: http://pastebin.com/TUhBKp7c
robenk
Hey Marcus thanks for the follow-up. Re threads, I don't think so, but I am not sure. I haven't worked with threads, so I don't know what to check for to be sure. But I doubt it, this project is just a slightly modified version of the a core data template. (I am not using NSThread for example).One thing I was curious about was whether NSFetchedResultController uses its own thread by default? I.e. if you a do a really big fetch, would it freeze the app till it finishes? I am curious because the way uitableviews work they seem like they could be populated asynchronously.
robenk
(Re pastebin, yeah I know I can update the question, but it seemed like adding a lot of boilerplate code. I don't know if that was the right decision though.)
robenk
A: 

I copied and pasted your code into the default Core Data navigation template, altered the entity to Reminder with a string attribute of textContent and it ran fine. There is nothing wrong with this code or the setup of the fetched results controller.

I think that the problem is actually with either your Reminder entity, the Reminder class or the textContent attribute or object. These errors could be caused by not being able to properly process the Reminder objects.


Edit:

Make sure you only run this test after some objects have been added to the context. It will crash if there are no objects. I'll test with a window-based template.

TechZen
Hi TechZen, thanks for the response. Note I said "If I use "Navigation-based application" the code works as expected." I tested this by importing the Reminder data model into the Core Data navigation template. In that context, the code works fine. What I am trying to do is get it working with a project created using the "Window-based application" template (iPad only app).Nonetheless, it's a good idea simplify the test by removing the Reminder entity, so I made a test application that did just that. It still failed, details in an update above. Thanks again.
robenk
+1  A: 

This is solved by using -[NSFetchedResultsController fetchedObjects] objectAtIndex:]

robenk