25 October 2007 · About 1 minute read

Unfaulting Core Data Objects After Load

Hours of searching for an answer were beginning to lead me to the conclusion that it was impossible to unfault transient properties of an NSManagedObject after loading them from a datafile.

The Problem: A ‘smart’ group to show expiring domains based on their (transient, calculated) isExpiring property worked until the document was saved. On saving, the objects in the context became faults and the smart group no longer worked. This was also true of re-loading a saved document - the domain’s isExpiring property would not be unfaulted.

The Solution: It’s a horrible solution, but seemingly the only way to get the faults to fire (calling [domain will/didAccessValueForKey:@”isExpiring”] did nothing). On awakening the smart group from a fetch, a quick NSFetchRequest is created to fetch all the Domain objects and unfault their @”data” propety. This is no doubt a dirty piece of code, but seems to force the calculated properties to be unfaulted.

```objc- (void)awakeFromFetch

{ [super awakeFromFetch]; [self commonAwake];

NSError *error; NSFetchRequest *fr = [[[NSFetchRequest alloc] init] autorelease]; [fr setEntity:[[self fetchRequest] entity]];

NSArray *results = [[self managedObjectContext] SEnumerator *enumerator = [results objectEnumerator]; id object;

while( object = [enumerator nextObject] ) { NSLog( [object valueForKey:@”domainName”] ); [object willChangeValueForKey:@”data”]; [object didChangeValueForKey:@”data”]; }

}```

With this now working, I can finally continue implementing the business logic in HM2.0, and stop worrying about the intricacies of Core Data and the dark art of Bindings.

Chris Blunt
Chris Blunt @cblunt
Chris is the founder of Plymouth Software. As well as code and business, he enjoys being a Dad, swimming, and the fine art of drinking tea.