0

I have this app I have been working on for a while, that I created using CoreData. However, I have recently started building a data model and working with CoreData. Now that I have done this, whenever I run the app in the simulator, it crashes in the App Delegate / didFinishLaunchingWithOptions on the first line:

self.window.rootViewController = self.navigationController;

The crash is:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'

If I comment out that line, it runs. Strange! I haven't done any work with CoreData at this point in the code yet - so no fetches yet. Perhaps there is some system fetch, because the data model changed? I have deleted the app from the Simulator and done a Clean/Build, but the issue persists.

First Question: How do I fix the error?

Second Question: Is this a problem, can I just ignore setting the rootViewController?

Third Question: Why is it failing now?

Jay Imerman
  • 4,475
  • 6
  • 40
  • 55
  • Somewhere you are doing a fetch request and your NSManagedObjectContext is probably null. Are you using a NSFetchedResultsController anywhere? – Mark McCorkle May 03 '13 at 21:22
  • That's the funny thing, @MarkM, it hasn't yet gotten to my fetches. I bet it is some data model update that hasn't been incorporated, in some automatic fetch that occurs during the assignment of the rootViewController property. I'm going through the CoreData tutorial, so I don't yet know how to manage data model updates in the app. – Jay Imerman May 08 '13 at 15:08
  • Do a "Find in Workspace" for fetch and place a breakpoint at every instance. You will find where the fetch is taking place. – Mark McCorkle May 08 '13 at 15:11
  • Great idea, unfortunately didn't pan out. There is a RootViewController class that does some fetches in a `fetchedResultsController` method, but the crash occurs after that. – Jay Imerman May 08 '13 at 15:19
  • What entity is the fetchedResultsController speaking to? You definitely have a fetch taking place and since NSFetchedResultsController also has a method to update when changes take place you probably want to check there. Is it being used for a tableViewController? – Mark McCorkle May 08 '13 at 15:23
  • Yeah, I put a breakpoint in that method (in RootViewController class), and that breakpoint isn't hit if I don't assign self.navigationController. So, I think they are related, but I can't find the line of code that is doing the fetch. Is there something else I need to do when I update the data model, to tell any code to use the new data model? I added some attributes to the entities, modified the relationships... – Jay Imerman May 08 '13 at 15:39
  • If you changed anything in the model you need to either do a lightweight migration and add a new model version or delete the app prior to loading it. But you will get a different error than what you are experiencing. It still sounds like the managedObjectContext is null to me. How are you passing it from controller to controller? – Mark McCorkle May 08 '13 at 15:41
  • Oh, I take that back! That is exactly where it is crashing, thanks! It is fetching an Event entity, which I probably deleted from the data model thinking I won't use it. This code was generated from the template, so I am going to have to think through how to fix it, but thanks for the pointer! – Jay Imerman May 08 '13 at 15:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29611/discussion-between-jay-imerman-and-markm) – Jay Imerman May 08 '13 at 15:51

2 Answers2

0

Depending on which project template you used, Xcode probably set up default fetch requests for you. As you edit the model you'll need to customize these.

For example, the "Master-Detail Application" template creates an NSFetchedResultsController in MasterViewController.m. It uses a fetch request that looks for the default Event entity type. If that entity doesn't exist, the fetch request will crash with the error you're seeing. I'm guessing that you deleted the default entity, created some of your own, but didn't change the code making the fetch request.

Commenting out that line prevents the crash but will also prevent the app from working. You're just skipping some crucial work, but you'll need to put that line back and fix the code.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Yeah, that's what I'm afraid of. I created the app in an older version of XCode 4, I'm pretty sure it was using the Single View Application template. I don't recall seeing the Master-Detail template. – Jay Imerman May 08 '13 at 15:10
  • Nothing to be afraid of, really. Make sure that any code that Xcode generated for you matches your data model. There's probably a mismatch, and it's probably not hard to find. – Tom Harrington May 08 '13 at 16:24
0

In my particular case, the template must have set up a default Event entity in the data model, and set up a default fetch controller to manage fetching batches of results, which is not at all what I need in my app. When I deleted the Event entity because it was irrelevant to the app, this caused it to crash when instantiating the generated View Controller. So I commented out the fetch controller code as not necessary.

Jay Imerman
  • 4,475
  • 6
  • 40
  • 55