NSManagedObject的自定义初始值设置 [英] Custom initializer for an NSManagedObject

查看:134
本文介绍了NSManagedObject的自定义初始值设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档:


您不应该覆盖init。您不应该重写initWithEntity:insertIntoManagedObjectContext:


,您应该改用awakeFromInsert或awakeFromFetch。



这是很好,如果我想做的是设置一些属性到当前日期或类似,但如果我想发送在另一个对象和基于其信息设置属性如何?



例如,在一个名为'Item'的NSManagedObject子类中,我想要一个initFromOtherThing:(Thing *)事件,其中项目的名称被设置为该事件的名称。我想避免只是必须记住每次创建项后立即设置名称,并且当我决定我想要项目也设置另一个默认属性基于Thing时,必须更新十五个不同的控制器类。

解决方案



/ div>

我认为最好的办法是通过继承NSManagedObject,然后创建一个类别来保存你想要添加到对象。例如一对类方法用于uniquing和方便地创建:

  +(item *)findItemRelatedToOtherThing:(Thing *)existingThing inManagedObjectContext *)context {
item * foundItem = nil;
//让NSFetchRequest看看这个项目是否已经存在...
return foundItem;
}

+(item *)itemWithOtherThing:(Thing *)existingThing inContext:(NSManagedObjectContext *)context {
item * theItem;
if(!(theItem = [self findItemRelatedToOtherThing:existingThing inManagedObjectContext:context])){
NSLog(@为Thing%@创建一个新项目,existingThing);
theItem = [NSEntityDescription insertNewObjectForEntityForName:@iteminManagedObjectContext:context];
theItem.whateverYouWant = existingThing.whateverItHas;
}
return theItem;
}



现在不要调用 initWithEntity:insertIntoManagedObjectContext: 直接使用你的方便类方法如:

  item * newItem = [item itemWithOtherThing:oldThing inContext :currentContext]; 


According to the docs:

You should not override init. You are discouraged from overriding initWithEntity:insertIntoManagedObjectContext:

and you should instead use awakeFromInsert or awakeFromFetch.

This is fine if all I want to do is set some attribute to the current date or similar, but what if I want to send in another object and set attributes based on its information?

For example, in an NSManagedObject subclass called 'Item', I want an initFromOtherThing:(Thing *)thing, in which the item's name is set to that of the thing. I would like to avoid 'just having to remember' to set the name each time immediately after creating the item, and having to update fifteen different controller classes when I decide that I want Item to also set another default attribute based on Thing. These are actions tied to the model.

How am I meant to handle this?

解决方案

I think the best way to handle this is by subclassing the NSManagedObject and then creating a category to hold what you want to add to the object. For example a couple of class methods for uniquing and conveniently creating:

+ (item *) findItemRelatedToOtherThing: (Thing *) existingThing inManagedObjectContext *) context {
    item *foundItem = nil;
    // Do NSFetchRequest to see if this item already exists...
    return foundItem;
}

+ (item *) itemWithOtherThing: (Thing *) existingThing inContext: (NSManagedObjectContext *) context {
    item *theItem;
    if( !(theItem = [self findItemRelatedToOtherThing: existingThing inManagedObjectContext: context]) ) {
        NSLog( @"Creating a new item for Thing %@", existingThing );
        theItem = [NSEntityDescription insertNewObjectForEntityForName: @"item" inManagedObjectContext: context];
        theItem.whateverYouWant = existingThing.whateverItHas;
    }
    return theItem; 
}

Now don't ever call initWithEntity:insertIntoManagedObjectContext: directly just use your convenience class method like:

item *newItem = [item itemWithOtherThing: oldThing inContext: currentContext];

这篇关于NSManagedObject的自定义初始值设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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