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

  • Home
  • SEARCH
  • 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 6611199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:56:22+00:00 2026-05-25T19:56:22+00:00

I have a Core Data entity that uses a custom subclass of NSManagedObject. I

  • 0

I have a Core Data entity that uses a custom subclass of NSManagedObject. I have a few instance variables and properties on it that aren’t associated with Core Data attributes or relationships. An example of the kind of thing I’m doing is this:

@interface Root : NSManagedObject
{
    Foo *foo;
    Bar *bar;
}

@property (nonatomic, retain) Foo *foo;
@property (nonatomic, retain) Bar *bar;

// Core Data generated
@property (nonatomic, retain) Node *relationship;
@property (nonatomic, retain) NSString *attribute;

-(void)awake;

@end


@implementation Root

@synthesize foo;
@synthesize bar;

@dynamic relationship;
@dynamic attribute;

-(void)awakeFromInsert {
    [super awakeFromInsert];
    [self awake];
}

-(void)awakeFromFetch {
    [super awakeFromFetch];
    [self awake];
}

-(void)awake {
    Foo *newFoo = [[Foo alloc] init];
    self.foo = newFoo;
    [newFoo release];

    // bar is not automatically initialized, but can be set by something external
}

-(void)didTurnIntoFault {
    [foo release];
    foo = nil;
    [bar release];
    bar = nil;

    [super didTurnIntoFault];
}

@end

Now in my app I get an instance of Root by a fetch request once when the app launches, and I retain it until the app quits. (Actually it’s a bit more complicated because you can delete the root instance and create a new one, but at most one exists at a time and it is retained.) So my hope is that didTurnIntoFault will never be called until my app quits. If it did, then at some point I would refer to root.foo or root.bar and get nil. That would be an error for my app. A Root instance should always have a non-nil value for foo and bar; foo is created whenever the instance is loaded, and bar is set by the caller immediately after fetching the root instance.

Can I rely on didTurnIntoFault not being called, if my code is retaining the NSManagedObject?

If I don’t want didTurnInfoFault to be called, why do I have it? Well, I have to clean up sometime. Maybe I should really put that code into dealloc, if I don’t want those instance variables released until the program quits. But I think I read some documentation that discouraged using dealloc for subclasses of NSManagedObject.

  • 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-05-25T19:56:23+00:00Added an answer on May 25, 2026 at 7:56 pm

    If your object exists in the underlying file store then it can potentially be faulted out at any time due to low memory warnings etc.

    But from your code it looks like all you need is a simple lazily-initialized property, i.e. override the ‘foo’ getter like this:

    - (Foo *)foo
    {
        if (!foo) {
            foo = [[Foo alloc] init];
        }
    
        return foo; // or [[foo retain] autorelease] if you need that safety
    }
    

    This is guaranteed not to return nil, and you can still release and nil out the instance variable if you want in didTurnIntoFault. The next time the getter is called a new ivar will be initialized again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my core data model I have a Person entity that has a to
I have an entity in core data that has 2 Attributes. One that is
I have an NSTreeController that manages an entity is a core data model and
I have set up a Core Data model that includes an entity, Item with
We have an app that uses Core Data. In the next version I would
I have a core data entity that represents the attributes of a product, as
I have a simple core-data entity that has a Boolean attribute called subscribedToNewsletter .
I have an entry on a core data entity that represents a NSTimeInterval. First
I have an entry on a core data entity that represents a NSTimeInterval. First
I have a Core Data entity with four boolean non-optional properties, defaulted to NO.

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.