Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

objective c - Custom initializer for an NSManagedObject

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...