我知道有人问过类似的问题,但它们都有不同的答案,似乎没有一个问题对我有帮助。
因此,我有一个名为TherapistScheduler的应用程序,它在项目根部的/Data文件夹中有therapistScheduler.xcdatamodel。该文件夹中有一个Session.h和Session.m文件,它们与我的数据模型中的会话实体匹配。
我的应用程序委托中有managedObjectContext、managedObjectModel和persistantStoreCoodinator等功能。
在我的applicationDidFinishLaunching方法中,我在loginViewController的顶部添加了一个视图控制器(loginViewController) (这是一个tabBarController),我正在尝试从那里访问会话实体。
我使用以下代码尝试保存一个值--这是抛出错误:
// create session
Session *session = (Session *) [NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:self.managedObjectContext];
[session setSessionHash:strUserhash]; H文件包含:
NSManagedObjectContext *managedObjectContext; // in the interface declaration
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; // after the interfaceM文件包含
@synthesize managedObjectContext;我需要在loginViewController viewDidLoad中做些什么来初始化managedObjectContext吗?我有点搞不懂这一切是怎么回事。我希望能够从选项卡栏上切换到的任何视图访问存储在managedObjectContext中的数据。
我知道我可能需要为这个问题添加更多的代码,但我不知道还需要什么。
我得到的错误是:
TherapistScheduler[40752:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Session''
*** Call stack at first throw:
(
0 CoreFoundation 0x02581919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0295b5de objc_exception_throw + 47
2 CoreData 0x0004525b +[NSEntityDescription entityForName:inManagedObjectContext:] + 187
3 CoreData 0x0007cd8b +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] + 59
4 TherapistScheduler 0x000030ce -[LoginViewController processLoginForUserhash:andType:] + 142
5 TherapistScheduler 0x000039fa -[LoginViewController connectionDidFinishLoading:] + 330
6 Foundation 0x01f91666 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
7 Foundation 0x01f915bf _NSURLConnectionDidFinishLoading + 133
8 CFNetwork 0x02b8d9f1 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 285
9 CFNetwork 0x02c56c72 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 402
10 CFNetwork 0x02c570ea _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1546
11 CFNetwork 0x02c570ea _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1546
12 CFNetwork 0x02b82dfe _ZN19URLConnectionClient13processEventsEv + 100
13 CFNetwork 0x02b82c95 _ZN17MultiplexerSource7performEv + 247
14 CoreFoundation 0x02562d7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
15 CoreFoundation 0x024c11dd __CFRunLoopDoSources0 + 333
16 CoreFoundation 0x024c07c6 __CFRunLoopRun + 470
17 CoreFoundation 0x024c0280 CFRunLoopRunSpecific + 208
18 CoreFoundation 0x024c01a1 CFRunLoopRunInMode + 97
19 GraphicsServices 0x02de62c8 GSEventRunModal + 217
20 GraphicsServices 0x02de638d GSEventRun + 115
21 UIKit 0x00215b58 UIApplicationMain + 1160
22 TherapistScheduler 0x00001f3d main + 125
23 TherapistScheduler 0x00001eb5 start + 53
24 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'发布于 2011-11-18 15:40:07
我不得不将managedObjectContext作为应用程序委托的属性,而不是self。所以它看起来像appDelegate.managedObjectContext。
发布于 2012-07-11 00:39:22
当您通过managedObjectContext时,我认为您所做的是这样的:
TimelineTableViewController * timelineTableViewController;
timelineTableViewController = [TimelineTableViewController alloc];
[timelineTableViewController initWithStyle:UITableViewStylePlain];
timelineTableViewController.managedObjectContext = self.managedObjectContext;
//...然后在TimelineTableViewController的viewDidLoad:方法中,您想通过managedObjectContext获取数据,是吗?
我调试了这个问题,发现TimelineTableViewController的viewDidLoad:将比timelineTableViewController.managedObjectContext = self.managedObjectContext; (从父级(或AppDelegate)传递managedObjectContext到子级)更快。因此,当您试图获取数据时,managedObjectContext实际上是nil。这就是为什么会出现这样的问题:
'+entityForName:无法找到实体名称‘Session’的NSManagedObjectModel
解决办法:
当您需要获取数据时,您需要做的是确保managedObjectContext不是零:
TimelineTableViewController * timelineTableViewController;
timelineTableViewController = [TimelineTableViewController alloc];
timelineTableViewController.managedObjectContext = self.managedObjectContext;
[timelineTableViewController initWithStyle:UITableViewStylePlain];
// ...我刚刚用这种方式解决了这个问题(不需要直接使用AppDelegate的managedObjectContext )。
https://stackoverflow.com/questions/7866591
复制相似问题