Objective c 如何在NSPersistentDocument和view controller之间共享NSManagedObjectContext?
OSX编程新手。从基于文档的应用程序的Xcode模板开始,该应用程序包含核心数据 在我的default Document.xib中,我创建了一个Objective c 如何在NSPersistentDocument和view controller之间共享NSManagedObjectContext?,objective-c,xcode,macos,cocoa,core-data,Objective C,Xcode,Macos,Cocoa,Core Data,OSX编程新手。从基于文档的应用程序的Xcode模板开始,该应用程序包含核心数据 在我的default Document.xib中,我创建了一个视图,我正在使用自定义视图控制器控制该视图。然后,我在Document.xib中创建了一个托管对象上下文,并创建了两个出口,一个出口到Document.h: @property (strong) IBOutlet NSManagedObjectContext *myManagedObjectContext; @property (weak) IBOut
视图
,我正在使用自定义视图控制器
控制该视图。然后,我在Document.xib中创建了一个托管对象上下文
,并创建了两个出口,一个出口到Document.h
:
@property (strong) IBOutlet NSManagedObjectContext *myManagedObjectContext;
@property (weak) IBOutlet NSManagedObjectContext *myManagedObjectContext;
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (retain) NSManagedObjectContext *managedObjectContext;
@property (retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
和一个到ViewController.h
:
@property (strong) IBOutlet NSManagedObjectContext *myManagedObjectContext;
@property (weak) IBOutlet NSManagedObjectContext *myManagedObjectContext;
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (retain) NSManagedObjectContext *managedObjectContext;
@property (retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
在Document.m中的windowControllerDidLoad
中,我随后添加了self.myManagedObjectContext=[self-managedObjectContext]
按照鬼蝇的建议()我添加了:
在WindowControllerIDLoad
和我的ViewController的awakeFromNib
中找到我的文档.m。在Document.m中,一切似乎都正常工作:所有NSLog语句看起来都正常,我也可以将实体添加到我的NSManagedObjectContext中,但在ViewController中,只有第一条NSLog语句工作,其余语句返回(null)
我的问题:这里出了什么问题,我的做法是否正确
各种其他问题似乎建议将AppDelegate添加到main menu.xib
,但我不确定这在基于文档的应用程序中实际如何工作(例如,我希望每个文档都有一个单独的NSManagedObjectContext
,但如果我使用AppDelegate,那么它们肯定都是一样的?),或者,即使建议这样做,因为一些教程明确指出这不是如何做到的(例如,在这里)。非常感谢您的帮助!谢谢。编辑:正确的答案是使用MagicalRecord()并为自己节省大量时间
我先前的答覆如下:
好吧,我想我已经做到了,但我不确定这是“最好的”还是“最惯用的”方法。我将非常详细地解释它,以防其他人偶然发现这个问题,并且同样被所有这些是如何结合在一起的问题难倒
首先,忘记.xib中的ManagedObjectContext;我不确定什么时候用这个合适
我创建了一个名为AppDelegate的应用程序委托。在AppDelegate.h
中:
@property (strong) IBOutlet NSManagedObjectContext *myManagedObjectContext;
@property (weak) IBOutlet NSManagedObjectContext *myManagedObjectContext;
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (retain) NSManagedObjectContext *managedObjectContext;
@property (retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end
请注意,我最初尝试将其放在WindowControllerIDloadNib
方法中,但这不起作用,因为这似乎是在我的ViewController
的awakeFromNib
方法之后调用的
然后,在我的ViewController.m
中的awakeFromNib
中,我可以通过访问上下文,例如:
AppDelegate *appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
MyEntity *test = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[appDelegate managedObjectContext]];
如果在接下来的几天里我没有发现任何问题,我会接受这个答案
一个悬而未决的问题是当我打开多个文档时会发生什么。它们是否共享相同的上下文?当您开始使用多个文档时,上面的答案会产生问题-这不起作用,因为您将只保存到最后打开的文档。您不应该尝试创建自己的托管对象上下文,因为这样您就必须为数据的存储位置等设置所有内容(这已经完成)。这要简单得多:
主文档类应该是NSPersistentDocument的子类。如果不是,那么您可以直接用它代替NSDocument。NSPersistentDocument有自己的managedObjectContext和persistentStoreCoordinator,您可以在文档中使用它们(存储文档相关数据)
因此,这意味着每个文档都有自己的managedObjectContext和persistentStoreCoordinator。要在其他类中访问这些,您应该传递对文档的引用。例如,在视图控制器子类上,为文档创建@IBOutlet,在interface builder中附加文档,然后在构造函数中,将文档的managedobjectcontext复制到视图控制器中自己的指针以供使用。为什么要在XIB中创建托管对象上下文?NSPersistentDocument
已经设置了一个供您使用的文档!沃尔克:可能是因为我不明白自己在做什么!我可以看到NSPersistentDocument为我设置了一个,但我不知道如何从我在Document.xib中使用的ViewController访问它…只是为了回答您最后的开放性问题。每个NSPersistentDocument都有自己的上下文,因为每个文档都有自己的持久存储。