从AppDelegate获取managedObjectContext [英] Getting managedObjectContext from AppDelegate

查看:377
本文介绍了从AppDelegate获取managedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Mac上的 AppDelegate 取得 managedObjectContext 平台(而不是iOS)做一些CoreData操作。在这里的许多答案已经指出(虽然主要是为iOS)有几个选项,如创建一个单例访问managedObjectContext或添加一个 managedObjectContext 属性到每个控制器

我做了后者,但是每次我尝试创建一个基于某个实体的managedObject时,我得到以下错误: + entityForName:nil不是合法的NSManagedObjectContext参数搜索实体

I'm trying to get the managedObjectContext from the AppDelegate on the mac platform (not iOS) to do some CoreData operations. As many answers here on SO already pointed out (although mostly for iOS) there are a couple of options like creating a singleton to access the managedObjectContext or adding a managedObjectContext property to every controller from which I want to access it.
I've done the latter but every time I try to create a managedObject based of some entity I get the following error: +entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity

这是我到目前为止做的:

ViewController.h:

Here's what I have done so far:
ViewController.h:

@interface HitpointsTableViewConroller : NSObject <NSTableViewDataSource> {
IBOutlet NSTableView *tableView;
NSMutableArray *list;
}

@property NSManagedObjectContext *managedObjectContext;

Init方法为该控制器获取 managedObjectContext

Init method for that controller to get the managedObjectContext

- (id)init {
if (self = [super init]) {
    // get the managedObjectContext
    NSManagedObjectContext *currentContext = [[NSApp delegate]managedObjectContext];
    self.managedObjectContext = currentContext;

    hitpointsList = [[NSMutableArray alloc]init];
}

return self;
}

我创建一个NSManagedObject的新实例的方法:

Method where I create a new instance of NSManagedObject:

- (IBAction)addItem:(id)sender {
  // create new item
  Item *item = (Item *)[NSEntityDescription   
  insertNewObjectForEntityForName:@"Item"   
  inManagedObjectContext:[self managedObjectContext]];

  // set defaults for that item
  [item setName:@"Coffee"];
  [item setPrice:2.99];

  // add hitpoint to list
   [list addObject:item];
   }

任何想法为什么这不起作用?

Any ideas why this is not working?

谢谢!

推荐答案

首先:你的属性没有属性。

First: your property has no attributes. AFAIK, "atomic" is the default then, which would be bad for most objects.

第二:如果你总是从应用程序委托中获取MOC,为什么要分配它到一个财产?在你的addItem方法中,只是从代理而不是属性获取你的MOC。

Second: if you always get the MOC from the app-delegate, why assign it to a property? in your addItem-method, just get your MOC from the delegate instead of the property.

第三:如果从代理获得MOC仍然导致nil错误,你显然需要在代理中检查你的方法。

Third: if getting the MOC from the delegate still results in the nil-error, you obviously need to check your methods in the delegate.

(原来是作为注释发布的,但由于注释不允许段落格式化,我将其重新编写为答案)

(originally posted this as a comment, but since comments don't allow for paragraph-formatting, I reposted it as an answer)

这篇关于从AppDelegate获取managedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆