Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed some help understanding faulting in Core Data
    text
    copied!<p>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:</p> <pre><code>@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 </code></pre> <p>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.</p> <p>Can I rely on didTurnIntoFault not being called, if my code is retaining the NSManagedObject?</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload