executeFetchRequest:error: A fetch request must have an entity

32,205

Solution 1

It seemed as if my data got corrupted, so I deleted my data model and the database in the iPhone simulator, and started fresh.

Solution 2

My issue is I didn't use the same name for Entity and Class. Trivial solution to fix it is by giving them the same name.

Solution 3

If you are using MagicalRecored with Swift:

Make sure you use @objc directive in the Swift NSManagedObject subclass to make the class accessible to Objective-C code from the MagicalRecord library

@objc(MyEntity)
class MyEntity: NSManagedObject {
    @NSManaged var myAttribute: Int16
}

Solution 4

After searching all over for a solution, what fixed it for me was doing a Clean/Build in Xcode.

Product->Clean, Product->Build, then try running it.

Solution 5

I had the same error.

For me, it is because I have added a new Model Version, but I did not set it as "Current Version". Careless me! To fix, select the xcdatamodel, click Design > Data Model > Set Current Version. The xcdatamodel file will then have a green tick.

Hopes that helps.

Share:
32,205
sudo rm -rf
Author by

sudo rm -rf

Hi, I'm Jonathan. I'm an iOS & Mac developer with a specialty in Core Animation. I created this Stack Overflow account back when I knew absolutely nothing about programming. Thus this profile serves as a documentation of my journey to where I am today. Glad I was able to help people out along the way. Keep up with me elsewhere: GitHub: https://github.com/jwilling Twitter. https://twitter.com/willing Blog: http://jwilling.com

Updated on August 16, 2020

Comments

  • sudo rm -rf
    sudo rm -rf over 3 years

    I had a project that was working fine. It had "Core Data" checked, and had the data model all set up. I only started adding a couple entities and properties tonight. When I try to run my app now, it won't even load. It just crashes on me.

    Here's the error:

    'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'

    I'm really scared because I don't have a backup of this and if I can't get it working I don't know what I'll do. :)

    Thanks in advance!

    EDIT: I got fed up with my data, so I just copied a new blank xcdatamodel to my project and I'm going to start fresh. Thanks for the help!

  • Nuthatch
    Nuthatch about 11 years
    yeah, don't forget to delete the app from the simulator!
  • Jay Imerman
    Jay Imerman about 11 years
    I both deleted the app in the Simulator, and did Clean/Build. Same problem, so this did not fix the issue for me. I do not want to delete and recreate a complex data model. So, how to correct the issue non-destructively?
  • Jake Stoeffler
    Jake Stoeffler about 11 years
    Why the down-vote, @JayImerman? I'm sorry that my answer didn't work for you, but that doesn't mean you should down-vote it. Did you try a "Reset Content and Settings" in the simulator? Does it work on a real device?
  • apb
    apb about 9 years
    Thanks! The entity file will get overwritten anytime you recreate the NSManagedObject subclass though. Is there a better way to do it than manually adding that line each time?
  • gaussblurinc
    gaussblurinc over 8 years
    is it a trivial solution? You can simply modify Default configuration (mapping: entity -> class) in data model.
  • Afzaal Ahmad
    Afzaal Ahmad almost 7 years
    app crash in iOS 9.3 but working with iOS 10.3 when i add @objc(MyEntity) as suggested it work on iOS 9.3 but app crash on iOS 10.3 any idea why it crash?
  • Roman
    Roman over 4 years
    Or let fetchRequest: NSFetchRequest<ReminderData> = fetchRequest() with no need to type a name of the entity explicitly (by using that function which you mentioned in Codegen file ReminderData+CoreDataProperties.swift)
  • amin torabi
    amin torabi over 3 years
    it says Aug 15, 2020