Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:51:25+00:00 2026-06-03T08:51:25+00:00

According to the docs: You should not override init. You are discouraged from overriding

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:51:26+00:00Added an answer on June 3, 2026 at 8:51 am

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to docs at http://code.google.com/p/minify/wiki/UriRewriting $min_serveOptions['rewriteCssUris'] = false; I should be able to add
According to the docs String text = String.format(%5.1e, 3.1415f); Should produce something like 3.1E+00
According to the gcc docs , memcmp is not an intrinsic function of GCC.
According to the Thinking Sphinx docs ... Turning on delta indexing does not remove
I am trying to use the Nancy addin Nancy.LightningCache According to the docs I
According to the DBUnit docs link it should support using a CLEAN_INSERT when using
According to the docs , using the CENTER attribute will not perform any scaling
According to this http://docs.nuget.org/docs/start-here/using-the-package-manager-console I should see the PM prompt and commands like get-packages
According to Riak's docs (using Python bindings), get_keys() is extremely expensive and not suitable
According to http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29 , Class.getDeclaredMethods() should only include methods declared by the class. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.