我有一个在根视图控制器上工作的CoreData数据库。我有第二个UIViewController,当我切换到并使用相同的行获取NSEntityDescription时,会得到以下错误:
由于未命名的异常“NSInternalInconsistencyException”终止应用程序,原因:+entityForName:未能找到实体名“Channel”的NSManagedObjectModel
我是这样做的:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Channel" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];该错误位于NSEntityDescription *entity行。如果这行在另一个UIViewController上工作,这怎么可能?
谢谢
发布于 2011-11-15 03:41:00
错误是暗示ManagedObjectContext有问题。
您是否将ManagedObjectContext从第一个视图传递到第二个视图(如果不尝试的话)
如果这不起作用,你能在分配self.context的地方发布代码吗
您指的是类似于appDelegate中awakeFromNib方法中的代码:
RootViewController *rootViewController = (RootViewController *)[self.navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;您需要第二行来传递实际的managedObjectContext
您可以将第二行放在firstView中,如下所示:
//secondViewController creation here
secondViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:secondViewController
animated:YES];当然,您需要设置必要的@property。
在从UIViewController到UIViewController的过程中,从appDelegate开始,您将传递对NSManagedObjectContext实例的引用。
如果您需要更多信息,UITabBarController类引用就在那里。
只需确保在分配任务时设置了IBOutlet,并且已经创建了NSManagedObject。
由选项卡条接口显示的根视图控制器的数组。 @属性(非原子,复制) NSArray *视图控件
发布于 2012-10-17 11:33:03
对我来说,解决问题的办法是手动加载NSEntityDescription,而不是使用来自NSFetchRequest的方便方法:
NSFetchRequest *fetchRequest = [[NSFetchRequest new] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:entity inManagedObjectContext:[self managedObjectContext]];https://stackoverflow.com/questions/8131053
复制相似问题