3

When my main view controller is loaded and it calls viewDidLoad I am performing a fetch request and retiring an array or my core data objects:

+ (NSArray *)getData {

    // Fetch Data
    NSError *error = nil;
    if (![[[AppDelegate instance] fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    NSArray *items = [[AppDelegate instance].fetchedResultsController fetchedObjects];
    return items;

}//end

For some reason, this returns an empty array when called from viewDidLoad.

If I call the same method from viewDidAppear: it works correctly and returns the NSArray of my CoreData objects.

Is there a reason why this won't work in viewDidLoad?

EDIT:

Fetched Results Controller method:

/**
 * The controller the gets our results from core data
 *
 * @version $Revision: 0.1
 */
- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    // Create and configure a fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SiteConfig" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Create the sort descriptors array
    NSSortDescriptor *sectionTitle = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sectionTitle, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management

    return fetchedResultsController;

}//end
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

1 Answers1

1

My guess is that your view controller is part of your MainWindow.xib, and so its viewDidLoad is being called before your app delegate gets the core data context ready.

You probably want to run this in viewWillAppear anyway, so that you'll get new data if you leave the screen and return. The other option would be to ensure the app delegate responds to fetchedResultsController by getting the core data stack ready if it isn't already.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • I would rather have my app delegate get it ready. Should I do something in `application:didFinishLaunchingWithOptions:` to prepare the `fetchedResultsController`? – Nic Hubbard Jul 04 '12 at 20:17
  • I believe, and you can confirm, that `application:didFinishLaunchingWithOptions:` is called after `viewDidLoad` of your view controller. How are you preparing the fetchedResultsController now? – Jesse Rusak Jul 04 '12 at 20:21
  • I updated my question with the `fetchedResultsController` code. – Nic Hubbard Jul 04 '12 at 21:58
  • Hrm. Assuming you're using the managedObjectContext method from Apple's Core Data project template, that looks OK. Are you doing any other setup in application:didFinishLaunchingWithOptions: that coudl affect this? Or in any other view controller's viewDidLoad or viewWillAppear methods? You might also try [enabling SQL debugging](http://stackoverflow.com/questions/6428630/xcode4-and-core-data-how-to-enable-sql-debugging) to see why your fetch isn't returning results. – Jesse Rusak Jul 05 '12 at 00:07
  • Yeah, not doing anything strange. At what point am I suppose to do a fetch? Doesn't my `getData` method handle that? Confused why the fetch wouldn't return anything... – Nic Hubbard Jul 05 '12 at 00:32
  • I'm also at a bit of a loss. I would think it makes more sense to do the fetch in viewWillAppear, but I really don't see any reason from the code you've shown that it wouldn't work in viewDidLoad. Does it actually return an empty array? Or nil? If it returns nil, what else is nil? The fetchedResultsController itself, for example? – Jesse Rusak Jul 05 '12 at 00:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13430/discussion-between-nic-hubbard-and-jesse-rusak) – Nic Hubbard Jul 05 '12 at 00:50