Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No NSEntityDescriptions in any model claim the NSManagedObject subclass

Tags:

ios

swift

I am new to CoreData and I'm trying to create a caching mechanism wherein after parsing objects from the API, I save them to the data model then fetch it again to show it on the tableview. I'm trying to fetch it using NSFetchedResultsController. Upon initialization of the NSFetchedResultsController, I'm encountering this runtime exception:

2018-12-09 15:03:20.493509+0800 [5184:148001] [error] error:  No NSEntityDescriptions in any model claim the NSManagedObject subclass  'Product' so +entity is confused.  Have you loaded your  NSManagedObjectModel yet ? CoreData: error: No NSEntityDescriptions in any model claim the  NSManagedObject subclass 'Product' so +entity is confused.  Have you  loaded your NSManagedObjectModel yet ? 2018-12-09 15:03:20.493718+0800[5184:148001] [error] error: +  [Product entity] Failed to find a unique match for an  NSEntityDescription to a managed object subclass CoreData: error: +[Product entity] Failed to find a unique match for an  NSEntityDescription to a managed object subclass 

What could be the reason why?

like image 332
dypbrg Avatar asked Dec 09 '18 08:12

dypbrg


People also ask

How do I create a subclass in NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.

How do I update my class in NSManagedObject?

You can do this: Choose "Create NSManagedObject Subclass…" from the Core Data editor menu to delete and recreate this implementation file for your updated model. You will then remove the files you already had, and the new ones will be created.

What is NSManagedObjectModel in Swift?

NSManagedObjectModel provides an API to retrieve a stored fetch request by name, and to perform variable substitution—see fetchRequestTemplate(forName:) and fetchRequestFromTemplate(withName:substitutionVariables:) . You typically define fetch request templates using the Data Model editor in Xcode.


2 Answers

If you ever encounter an issue similar to this using SwiftUI, you can try changing the entity's class module from Global Namespace to Current Product Module.

Go to your xcdatamodeld file and select the problematic entity. Then in the data model inspector, change the Module field from the default Global namespace to available value "Current Product Module" by clicking on the arrow at the right of the field.

This allowed my app to compile without encountering the error.

Changing the module field

like image 144
Pomme2Poule Avatar answered Sep 23 '22 12:09

Pomme2Poule


My experience:

The entity class was missing the @objc(Person) line above the class name. I do have more classes that are working without this line but only when creating this specific entity, I have got this error.

@objc(Person) public class Person: NSManagedObject {  } 
like image 31
shbedev Avatar answered Sep 19 '22 12:09

shbedev