ios 无法找到 NSManagedObjectModel 、 NSInternalConsistencyException

发布于 2024-12-11 05:22:50 字数 4205 浏览 0 评论 0原文

我知道有人问过类似的问题,但他们都有不同的答案,而且似乎没有一个对我有帮助。

因此,我有一个名为 TherapistScheduler 的应用程序,它在项目根目录的 /Data 文件夹中包含 therapyScheduler.xcdatamodel。该文件夹内有一个 Session.h 和 Session.m 文件,它们与我的数据模型中的会话实体相匹配。

我的应用程序委托中包含 managedObjectContextmanagementObjectModelpersistantStoreCoodinator 函数。

在我的 applicationDidFinishLaunching 方法中,我在 rootViewController (它是一个 tabBarController)的顶部添加了一个视图控制器(loginViewController),并且我试图从那里访问我的 Session 实体。

我正在使用以下代码来尝试保存值 - 这会引发错误:

  // create session 
Session *session = (Session *) [NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:self.managedObjectContext];  
[session setSessionHash:strUserhash]; 

LoginViewController.h 文件包含:

NSManagedObjectContext *managedObjectContext; // in the interface declaration

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; // after the interface

LoginViewController.m 文件包含

@synthesize managedObjectContext;

我需要在 loginViewController viewDidLoad 中执行某些操作来初始化 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'

I know similar questions have been asked but they all have different answers and none seem to help me.

So, I've got an app called TherapistScheduler, which has therapistScheduler.xcdatamodel in a /Data folder at the root of the project. Inside that folder is a Session.h and Session.m file, which match up with my Session entity in my data model.

My app delegate has the functions managedObjectContext, managedObjectModel, and persistantStoreCoodinator in it.

Inside my applicationDidFinishLaunching method, I've added a view controller (loginViewController) over the top of my rootViewController (which is a tabBarController), and I'm trying to access my Session entity from there.

I'm using the following code to try and save a value - this is throwing the error:

  // create session 
Session *session = (Session *) [NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:self.managedObjectContext];  
[session setSessionHash:strUserhash]; 

The loginViewController.h file contains:

NSManagedObjectContext *managedObjectContext; // in the interface declaration

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; // after the interface

The loginViewController.m file contains

@synthesize managedObjectContext;

Do I need to do something in the loginViewController viewDidLoad to initialise the managedObjectContext? I'm a bit lost as to how this all works. I want to be able to access the data stored in the managedObjectContext from any view I switch to on the tab bar.

I know I'll probably need to add more code to the question, but I don't know what else is needed.

The error I'm getting is this:

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖鼻大婶 2024-12-18 05:22:50

我必须将 ManagedObjectContext 引用为应用程序委托的属性,而不是 self。所以它看起来像appDelegate.managementObjectContext

I had to refer to the managedObjectContext as a property of the app delegate not self. So it looked like appDelegate.managedObjectContext.

卸妝后依然美 2024-12-18 05:22:50

当你传递managedObjectContext时,我认为你所做的是这样的:

TimelineTableViewController * timelineTableViewController;
timelineTableViewController = [TimelineTableViewController alloc];
[timelineTableViewController initWithStyle:UITableViewStylePlain];
timelineTableViewController.managedObjectContext = self.managedObjectContext;
//...

然后在TimelineTableViewControllerviewDidLoad:方法中,你想要获取数据由 managedObjectContext 提供,是吧?

我调试了这个问题,发现 TimelineTableViewControllerviewDidLoad: 的调度速度会比 timelineTableViewController.managedObjectContext = self.managementObjectContext; (您传递的位置)更快ManagedObjectContext 从父级(或 AppDelegate)到子级)。因此,当您尝试获取数据时,managementObjectContext 实际上是nil。这就是你遇到这个问题的原因:

'+entityForName:无法找到实体名称“Session”的 NSManagedObjectModel'


解决方法:

您需要做的是确保当您需要获取数据时 managedObjectContext 不为 nil:

TimelineTableViewController * timelineTableViewController;
timelineTableViewController = [TimelineTableViewController alloc];
timelineTableViewController.managedObjectContext = self.managedObjectContext;
[timelineTableViewController initWithStyle:UITableViewStylePlain];
// ...

我只是通过这种方式解决了这个问题(不需要直接使用AppDelegate的managedObjectContext)。

When you passed the managedObjectContext, I think what you did is like this:

TimelineTableViewController * timelineTableViewController;
timelineTableViewController = [TimelineTableViewController alloc];
[timelineTableViewController initWithStyle:UITableViewStylePlain];
timelineTableViewController.managedObjectContext = self.managedObjectContext;
//...

and then in TimelineTableViewController's viewDidLoad: method, you wanted to fetch the data by managedObjectContext, huh?

I debugged this issue and found that TimelineTableViewController's viewDidLoad: will be dispatched quicker than timelineTableViewController.managedObjectContext = self.managedObjectContext; (where you passed the managedObjectContext from parent (or the AppDelegate) to child). So when you were trying to fetch the data, the managedObjectContext is nil actually. That's why you got this issue:

'+entityForName: could not locate an NSManagedObjectModel for entity name 'Session''


WORKAROUND:

What you have to do is to make sure that the managedObjectContext is not nil when you need to fetch the data:

TimelineTableViewController * timelineTableViewController;
timelineTableViewController = [TimelineTableViewController alloc];
timelineTableViewController.managedObjectContext = self.managedObjectContext;
[timelineTableViewController initWithStyle:UITableViewStylePlain];
// ...

I just solved this issue by this way (no need to use AppDelegate's managedObjectContext directly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文