Cocoa 在没有托管对象上下文的情况下无法执行操作
我有一个基于 Mac 窗口的应用程序,使用 CoreData 和 Cocoa 绑定将 CoreData 实体绑定到 NSArrayController 以在 NSCollectionView 中显示。
我的控制器上有一个名为 ManagedObjectConext 的属性(设置为视图文件所有者)。在 awakeFromNib 方法中,我尝试将 ManagedObjectContext (MOC) 属性设置为:
managedObjectContext = [(MyApplicationAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = [[NSApp delegate] managedObjectContext];
我还将 NSArrayControllers MOC 绑定配置为“文件所有者”MOC 属性,并将属性绑定配置为我的 CoreData 实体的 MOC 属性。
但是,每次我在应用程序即将进行绑定之前运行该应用程序时,都会收到以下错误消息:
Cocoa 无法在没有托管对象上下文的情况下执行操作
我已登录控制台检查 MOC 是否不为零,我认为它不是,因为这是来自 MOC 对象的 NSLog: myMoc = NSManagedObjectContext: 0x10052f9c0
我现在完全被难住了令人沮丧的是,本来应该如此简单的事情却占用了我这么多的时间!任何帮助将不胜感激。
I have a Mac window based application using CoreData and Cocoa bindings to bind a CoreData entity to an NSArrayController for displaying in a NSCollectionView.
I have a property on my controller (which is set as the views Files Owner) called managedObjectConext. In the awakeFromNib method I have tried setting the managedObjectContext (MOC) property to:
managedObjectContext = [(MyApplicationAppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = [[NSApp delegate] managedObjectContext];
I have also configured the NSArrayControllers MOC bindings to the 'Files Owner' MOC property and attribute bindings to that of my CoreData entity.
However everytime I run the application just before its about to do the binding I get the follwowing error message:
Cocoa Cannot perform operation without a managed object context
I have logged in the console to check that the MOC is not nil, which I presume it isn't as this is the NSLog from the MOC object: myMoc = NSManagedObjectContext: 0x10052f9c0
I am totally stumped now and frustrated that something that should be so simple has taken up so much of my time! Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
绑定是在 nib 中定义的,因此如果 nib 不知道控制器的 ManagedObjectContext 属性,则绑定也不知道。
在界面编辑器中,您需要将数组控制器的绑定面板中的 ManagedObjectContext 键绑定到特定上下文。
对于单上下文应用程序执行此操作的最简单方法是将应用程序委托中的 ManagedObjectContext 属性标记为 IBOutlet,以便它显示在界面编辑器中。然后直接从阵列控制器的面板绑定它。
老实说,我不知道为什么他们不在 Xcode 模板本身中这样做。你最终会经常这样做。
Bindings are defined in the nib, so if the nib doesn't know about the managedObjectContext property of the controller, then the bindings don't either.
In the interface editor, you need to bind the managedObjectContext key in the array controller's bindings panel to the specific context.
The easiest way to do this for a one context app, is to label the managedObjectContext property in the app delegate as an IBOutlet so that it shows up in the interface editor. Then just bind it directly from the array controller's panel.
Honestly, I don't know why they don't do that in the Xcode template itself. You end up doing it so often.
我也有同样的问题。这解决了这个问题:
将以下行:
从
awakeFromNib
方法移动到init
方法中。I had the same problem. This fixed it:
Move the lines:
from the
awakeFromNib
method into theinit
method.快速地将其添加到 ArrayController 附加到的任何视图控制器中:
In swift, add this to whatever view controller the ArrayController is attached to:
使用 Swift 4.x,您可以在 ViewController 中创建一个 var,如下所示:
然后将其绑定到您的 ArrayController :
With Swift 4.x you can create a var in ViewController like so:
Then bind it to your ArrayController :