我是否可以在不通过NSEntityDescription的情况下创建自定义托管对象类的新实例?

时间:2022-09-25 18:43:32

From an Apple example, I have this:

从Apple的例子来看,我有这个:

Event *event = (Event*)[NSEntityDescription 
    insertNewObjectForEntityForName:@"Event" 
             inManagedObjectContext:self.managedObjectContext];

Event inherits from NSManagedObject. Is there a way to avoid this weird call to NSEntityDescription and instead just alloc+init somehow directly the Event class? Would I have to write my own initializer that just does that stuff above? Or is NSManagedObject already intelligent enough to do that?

事件继承自NSManagedObject。有没有办法避免这种奇怪的NSEntityDescription调用,而只是直接在事件类中分配+ init?我是否必须编写自己的初始化程序才能执行上述操作?或者NSManagedObject已经足够智能了吗?

4 个解决方案

#1


5  

NSManagedObject provides a method called initWithEntity:insertIntoManagedObjectContext:. You can use this to do a more traditional alloc/init pair. Keep in mind that the object this returns is not autoreleased.

NSManagedObject提供了一个名为initWithEntity的方法:insertIntoManagedObjectContext:。您可以使用它来执行更传统的alloc / init对。请记住,返回的对象不是自动释放的。

#2


3  

I've run into the exact same problem. It turns out you can completely create an entity and not add it to the store at first, then make some checks on it and if everything is good insert it into the store. I use it during an XML parsing session where I only want to insert entities once they have been properly and entirely parsed.

我遇到了完全相同的问题。事实证明,您可以完全创建一个实体,而不是先将其添加到商店,然后对其进行一些检查,如果一切正常,请将其插入商店。我在XML解析会话期间使用它,我只想在实体和完全解析后插入实体。

First you need to create the entity:

首先,您需要创建实体:

// This line creates the proper description using the managed context and entity name. 
// Note that it uses the managed object context
NSEntityDescription *ent = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:[self managedContext]];

// This line initialized the entity but does not insert it into the managed object context.    
currentEntity = [[Location alloc] initWithEntity:ent insertIntoManagedObjectContext:nil];

Then once you are happy with the processing you can simply insert your entity into the store:

然后,一旦您对处理感到满意,您只需将实体插入商店即可:

[self managedContext] insertObject:currentEntity

Note that in those examples the currentEntity object has been defined in a header file as follows:

请注意,在这些示例中,currentEntity对象已在头文件中定义,如下所示:

id currentEntity

#3


1  

To get it to work properly, there is a LOT of stuff to do. -insertNewObject:... is by far the easiest way, weird or not. The documentation says:

为了让它正常工作,有很多事情要做。 -insertNewObject:...是迄今为止最简单的方法,奇怪与否。文件说:

A managed object differs from other objects in three main ways—a managed object ... Exists in an environment defined by its managed object context ... there is therefore a lot of work to do to create a new managed object and properly integrate it into the Core Data infrastructure ... you are discouraged from overriding initWithEntity:insertIntoManagedObjectContext:

托管对象在三个主要方面与其他对象不同 - 托管对象...在由托管对象上下文定义的环境中存在...因此,要创建新托管对象并正确集成它,还有很多工作要做进入核心数据基础架构......不鼓励您覆盖initWithEntity:insertIntoManagedObjectContext:

That said, you can still do it (read further down the page to which I linked) but your goal appears to be "easier" or "less weird". I'd say the method you feel is weird is actually the simplest, most normal way.

也就是说,您仍然可以这样做(在我链接的页面下方进一步阅读),但您的目标似乎“更容易”或“不那么奇怪”。我说你觉得奇怪的方法实际上是最简单,最正常的方法。

#4


1  

I found a definitive answer from More iPhone 3 Development by Dave Mark and Jeff LeMarche.

我从Dave Mark和Jeff LeMarche的更多iPhone 3开发中找到了明确的答案。

If it really bothers you that you use a method on NSEntityDescrpiton rather than on NSManagedObjectContext to insert a new object into an NSManagedObjectContext, you can use a category to add an instance method to NSManagedObjectContext.

如果您真的困扰您在NSEntityDescrpiton而不是NSManagedObjectContext上使用方法将新对象插入NSManagedObjectContext,则可以使用类别向NSManagedObjectContext添加实例方法。

Create two new text files called NSManagedObject-Insert.h and NSManagedObject-Insert.m.

创建两个名为NSManagedObject-Insert.h和NSManagedObject-Insert.m的新文本文件。

In NSManagedObject-Insert.h, place the following code:

在NSManagedObject-Insert.h中,放置以下代码:

import <Cocoa/Cocoa.h>
@interface NSManagedObjectContext (insert)
- (NSManagedObject *)insertNewEntityWithName:(NSString *)name;
@end

In NSManagedObject-Insert.m, place this code:

在NSManagedObject-Insert.m中,放置以下代码:

#import "NSManagedObjectContext-insert.h"

@implementation NSManagedObjectContext (insert)
- (NSManagedObject *)insertNewEntityWithName:(NSString *)name
{
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:self];
}
@end

You can import NSManagedObject-Insert.h anywhere you wish to use this new method. Then replace the insert calls against NSEntityDescription, like this one:

您可以在希望使用此新方法的任何位置导入NSManagedObject-Insert.h。然后用NSEntityDescription替换插入调用,如下所示:

NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

with the shorter and more intuitive one:

更短更直观的一个:

[context insertNewEntityWithName:[entity name]];

Aren't categories grand?

不是类别盛大吗?

#1


5  

NSManagedObject provides a method called initWithEntity:insertIntoManagedObjectContext:. You can use this to do a more traditional alloc/init pair. Keep in mind that the object this returns is not autoreleased.

NSManagedObject提供了一个名为initWithEntity的方法:insertIntoManagedObjectContext:。您可以使用它来执行更传统的alloc / init对。请记住,返回的对象不是自动释放的。

#2


3  

I've run into the exact same problem. It turns out you can completely create an entity and not add it to the store at first, then make some checks on it and if everything is good insert it into the store. I use it during an XML parsing session where I only want to insert entities once they have been properly and entirely parsed.

我遇到了完全相同的问题。事实证明,您可以完全创建一个实体,而不是先将其添加到商店,然后对其进行一些检查,如果一切正常,请将其插入商店。我在XML解析会话期间使用它,我只想在实体和完全解析后插入实体。

First you need to create the entity:

首先,您需要创建实体:

// This line creates the proper description using the managed context and entity name. 
// Note that it uses the managed object context
NSEntityDescription *ent = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:[self managedContext]];

// This line initialized the entity but does not insert it into the managed object context.    
currentEntity = [[Location alloc] initWithEntity:ent insertIntoManagedObjectContext:nil];

Then once you are happy with the processing you can simply insert your entity into the store:

然后,一旦您对处理感到满意,您只需将实体插入商店即可:

[self managedContext] insertObject:currentEntity

Note that in those examples the currentEntity object has been defined in a header file as follows:

请注意,在这些示例中,currentEntity对象已在头文件中定义,如下所示:

id currentEntity

#3


1  

To get it to work properly, there is a LOT of stuff to do. -insertNewObject:... is by far the easiest way, weird or not. The documentation says:

为了让它正常工作,有很多事情要做。 -insertNewObject:...是迄今为止最简单的方法,奇怪与否。文件说:

A managed object differs from other objects in three main ways—a managed object ... Exists in an environment defined by its managed object context ... there is therefore a lot of work to do to create a new managed object and properly integrate it into the Core Data infrastructure ... you are discouraged from overriding initWithEntity:insertIntoManagedObjectContext:

托管对象在三个主要方面与其他对象不同 - 托管对象...在由托管对象上下文定义的环境中存在...因此,要创建新托管对象并正确集成它,还有很多工作要做进入核心数据基础架构......不鼓励您覆盖initWithEntity:insertIntoManagedObjectContext:

That said, you can still do it (read further down the page to which I linked) but your goal appears to be "easier" or "less weird". I'd say the method you feel is weird is actually the simplest, most normal way.

也就是说,您仍然可以这样做(在我链接的页面下方进一步阅读),但您的目标似乎“更容易”或“不那么奇怪”。我说你觉得奇怪的方法实际上是最简单,最正常的方法。

#4


1  

I found a definitive answer from More iPhone 3 Development by Dave Mark and Jeff LeMarche.

我从Dave Mark和Jeff LeMarche的更多iPhone 3开发中找到了明确的答案。

If it really bothers you that you use a method on NSEntityDescrpiton rather than on NSManagedObjectContext to insert a new object into an NSManagedObjectContext, you can use a category to add an instance method to NSManagedObjectContext.

如果您真的困扰您在NSEntityDescrpiton而不是NSManagedObjectContext上使用方法将新对象插入NSManagedObjectContext,则可以使用类别向NSManagedObjectContext添加实例方法。

Create two new text files called NSManagedObject-Insert.h and NSManagedObject-Insert.m.

创建两个名为NSManagedObject-Insert.h和NSManagedObject-Insert.m的新文本文件。

In NSManagedObject-Insert.h, place the following code:

在NSManagedObject-Insert.h中,放置以下代码:

import <Cocoa/Cocoa.h>
@interface NSManagedObjectContext (insert)
- (NSManagedObject *)insertNewEntityWithName:(NSString *)name;
@end

In NSManagedObject-Insert.m, place this code:

在NSManagedObject-Insert.m中,放置以下代码:

#import "NSManagedObjectContext-insert.h"

@implementation NSManagedObjectContext (insert)
- (NSManagedObject *)insertNewEntityWithName:(NSString *)name
{
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:self];
}
@end

You can import NSManagedObject-Insert.h anywhere you wish to use this new method. Then replace the insert calls against NSEntityDescription, like this one:

您可以在希望使用此新方法的任何位置导入NSManagedObject-Insert.h。然后用NSEntityDescription替换插入调用,如下所示:

NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

with the shorter and more intuitive one:

更短更直观的一个:

[context insertNewEntityWithName:[entity name]];

Aren't categories grand?

不是类别盛大吗?