【发布时间】:2013-11-10 19:00:20
【问题描述】:
本项目使用 Mogenerator 和 Magical Record。我已经找到了一个错误,即awakeFromInsert 被调用了两次。我假设我的每个上下文都有一次。这是一个问题,因为我需要像这样在这个 NSManagedObject 上监听 NSNotifications:
- (void)awakeFromInsert
{
// Listen for a return from background mode
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteringForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
但是 awakeFromInsert get 被调用了两次,这很烦人。我想在第一次创建 NSManagedObject 时调用一次方法。
搜索this solution seems to make a lot of sense后。但是,当使用 Mogenerator 和 MagicalRecord 时,我看不到如何将类别添加到 NSManagedObject 上。没有一些复杂的覆盖。
在 MagicalRecord 中MR_createEntity 调用
if ([self respondsToSelector:@selector(insertInManagedObjectContext:)])
{
id entity = [self performSelector:@selector(insertInManagedObjectContext:) withObject:context];
return entity;
}
这个问题有更简洁的解决方案吗?
【问题讨论】:
-
Mogenerator 为每个实体创建两个类,一个“机器”类和一个“人类”类。您可以根据需要向“人类”类添加方法,而无需类别。
-
谢谢。是的,我已经这样做了。我想这里的问题是我需要 MagicalRecord 来调用我的新方法,而不是如上所示的 insertInManagedObjectContext。
-
我还不明白“awakeFromFetch 调用了两次”问题和
insertInManagedObjectContext:(这可能是我的错)之间的关系。 - 但是你不能在“人类”实体类中覆盖insertInManagedObjectContext:吗? -
对不起马丁,我的错误代码示例应该都显示 awakeFromInsert。由 insertInManagedObjectContext: 调用,然后对于 MR 保存的每个上下文。到目前为止,AFAIK。
-
似乎用 Mogenerator 覆盖 insertInManagedObjectContext: 效果不太好 - stackoverflow.com/questions/16430005/…
标签: objective-c core-data mogenerator magicalrecord-2.1