CoreData错误:+ entityForName:无法找到实体名称的NSManagedObjectModel

时间:2022-06-11 10:41:37

I've been struggling with CoreData for a few days, but I keep getting this error:

我已经在CoreData上苦苦挣扎了几天,但我一直收到这个错误:

'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name.

'NSInternalInconsistencyException',原因:'+ entityForName:无法找到实体名称的NSManagedObjectModel。

I have checked the entity name and what I wrote on my code and they're the same. I also recreated the object data-model and even delete the app from the simulator but nothing seems to fix it. Here's what I have:

我检查了实体名称和我在代码上写的内容,它们是相同的。我还重新创建了对象数据模型,甚至从模拟器中删除了应用程序,但似乎没有什么能解决它。这就是我所拥有的:

method to save into CoreData:

保存到CoreData的方法:

-(IBAction)save:(id)sender {
    NSManagedObject * newNews = [NSEntityDescription insertNewObjectForEntityForName:@"NewsStand"
    inManagedObjectContext:coredata.managedObjectContext];
    [newNews setValue:news_title forKey:@"story_title"];
    [newNews setValue:news_desc forKey:@"story_desc"];
    [newNews setValue:news_image  forKey:@"story_image"];
    [newNews setValue:test  forKey:@"story_url"];
    [coredata commit];
    NSLog(@"data saved!!!!");
}

I have implemented all methods of core data in a separated class:

我已经在一个单独的类中实现了核心数据的所有方法:

applicationDocumentsDirectory,  
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator, 
- (NSManagedObjectModel *)managedObjectModel, 
- (NSManagedObjectContext *)managedObjectContext

6 个解决方案

#1


25  

This is a fairly common error and it has three causes:

这是一个相当常见的错误,它有三个原因:

  1. Misspelling the entity name e.g. NewsStand instead of NewsStands.
  2. 拼错实体名称,例如NewsStand而不是NewsStands。

  3. Having a nil managed object context
  4. 拥有一个nil托管对象上下文

  5. Having no or the wrong managed object model loaded.
  6. 没有加载或错误的托管对象模型。

(1) is the most common but (3) is more common than (2). You can check that you are loading the right model with the keypath:

(1)是最常见的但(3)比(2)更常见。您可以使用keypath检查是否正在加载正确的模型:

aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities

then check the entity's names.

然后检查实体的名称。

#2


2  

During my development, I could not find Entities that I added later on. What worked for me:

在我的开发过程中,我找不到我后来添加的实体。什么对我有用:

Uninstall the App EVERY TIME you change the Data Model!

每次更改数据模型时卸载应用程序!

The Data Model is cached by Core Data between installs, to make sure the integrity stays in tact. Delete the App from the simulator / iPhone to be able to test your changes.

Core Data在安装之间缓存数据模型,以确保完整性保持完整。从模拟器/ iPhone中删除应用程序,以便能够测试您的更改。

PS: does anyone know how to do that automatically?

PS:有谁知道如何自动完成?

#3


2  

I had a similar problem and found TechZen's answer to be helpful (especially the suggestion to check the entities). However, my problem turned out to be a variant of (2): I could see that the moc itself wasn't nil, but I hadn't set the persistent store coordinator.

我有类似的问题,发现TechZen的答案是有帮助的(特别是检查实体的建议)。但是,我的问题变成了(2)的变体:我可以看到moc本身不是nil,但我没有设置持久性存储协调器。

[aManagedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]

or similar.

I'd add this as a comment to TechZen's answer, but can't yet, and want to include it in case someone else has my problem.

我将此作为对TechZen答案的评论添加,但还不能,并希望包含它以防其他人有我的问题。

#4


1  

Ensure that coredata.managedObjectContext is not nil.

确保coredata.managedObjectContext不是nil。

#5


0  

Don't have the rep to comment - but Luc Bloom's response fixed MY issue. I completely forgot I changed some stuff in the data model after initial build/install and spent entirely too long banging my head against the desk.

没有代表发表评论 - 但Luc Bloom的回应修正了我的问题。我完全忘记了在初始构建/安装后我在数据模型中更改了一些东西,并且花了很长时间撞到桌面上。

#6


0  

If you're editing a framework and running a unit test to get the error, make sure your xcdatamodeld file is added to the test target. Frameworks behave differently than normal projects.

如果您正在编辑框架并运行单元测试以获取错误,请确保将xcdatamodeld文件添加到测试目标。框架的行为与普通项目不同。

#1


25  

This is a fairly common error and it has three causes:

这是一个相当常见的错误,它有三个原因:

  1. Misspelling the entity name e.g. NewsStand instead of NewsStands.
  2. 拼错实体名称,例如NewsStand而不是NewsStands。

  3. Having a nil managed object context
  4. 拥有一个nil托管对象上下文

  5. Having no or the wrong managed object model loaded.
  6. 没有加载或错误的托管对象模型。

(1) is the most common but (3) is more common than (2). You can check that you are loading the right model with the keypath:

(1)是最常见的但(3)比(2)更常见。您可以使用keypath检查是否正在加载正确的模型:

aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities

then check the entity's names.

然后检查实体的名称。

#2


2  

During my development, I could not find Entities that I added later on. What worked for me:

在我的开发过程中,我找不到我后来添加的实体。什么对我有用:

Uninstall the App EVERY TIME you change the Data Model!

每次更改数据模型时卸载应用程序!

The Data Model is cached by Core Data between installs, to make sure the integrity stays in tact. Delete the App from the simulator / iPhone to be able to test your changes.

Core Data在安装之间缓存数据模型,以确保完整性保持完整。从模拟器/ iPhone中删除应用程序,以便能够测试您的更改。

PS: does anyone know how to do that automatically?

PS:有谁知道如何自动完成?

#3


2  

I had a similar problem and found TechZen's answer to be helpful (especially the suggestion to check the entities). However, my problem turned out to be a variant of (2): I could see that the moc itself wasn't nil, but I hadn't set the persistent store coordinator.

我有类似的问题,发现TechZen的答案是有帮助的(特别是检查实体的建议)。但是,我的问题变成了(2)的变体:我可以看到moc本身不是nil,但我没有设置持久性存储协调器。

[aManagedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]

or similar.

I'd add this as a comment to TechZen's answer, but can't yet, and want to include it in case someone else has my problem.

我将此作为对TechZen答案的评论添加,但还不能,并希望包含它以防其他人有我的问题。

#4


1  

Ensure that coredata.managedObjectContext is not nil.

确保coredata.managedObjectContext不是nil。

#5


0  

Don't have the rep to comment - but Luc Bloom's response fixed MY issue. I completely forgot I changed some stuff in the data model after initial build/install and spent entirely too long banging my head against the desk.

没有代表发表评论 - 但Luc Bloom的回应修正了我的问题。我完全忘记了在初始构建/安装后我在数据模型中更改了一些东西,并且花了很长时间撞到桌面上。

#6


0  

If you're editing a framework and running a unit test to get the error, make sure your xcdatamodeld file is added to the test target. Frameworks behave differently than normal projects.

如果您正在编辑框架并运行单元测试以获取错误,请确保将xcdatamodeld文件添加到测试目标。框架的行为与普通项目不同。