Core Data Minefield

Boisy Pitre boisy at boisypitre.com
Sun Feb 4 16:59:32 PST 2007


Thanks Marcus and Scott for the advice and wisdom.  I think I will  
stick to using primitiveValueForKey for now.

Another issue has cropped up regarding the loading of a previously  
saved data file in my Core Data application.  A quick review of the  
classes involved:

	FARM <-->> ANIMALS <-->> VACCINATIONS

ANIMALS has a transient attribute that is calculated based on  
attributes in VACCINATIONS.  Also, FARM has a transient attribute  
that is calculated based on attributes in ANIMALS.

Apple's Core Data application states if an entity wants to be aware  
of changes made to an attribute in another entity, it can register  
for notifications.  As per Apple's suggestion to solve this, I have  
the following code in both FARM.m and ANIMALS.m:

/*
     Called from awakeFromInsert and awakeFromFetch. Here we register  
for the
     notification for changes to the managed object context.
*/
- (void)commonAwake
{
     [[NSNotificationCenter defaultCenter] addObserver:self  
selector:@selector(refresh:)
         name:NSManagedObjectContextObjectsDidChangeNotification  
object:[self managedObjectContext]];
}


- (void)awakeFromInsert
{
	[super awakeFromInsert];

	[self setValue:[NSNumber numberWithInt:2] forKey:@"timeIntervalUnit"];

	[self commonAwake];
}


- (void)awakeFromFetch
{
	[super awakeFromFetch];

	[self commonAwake];
}


The refresh method referenced in commonAwake checks for the expected  
notification and recomputes its transient if so.

What I am seeing is that when a previously saved data file is opened,  
the inserted/deleted/updated  
NSManagedObjectContextObjectsDidChangeNotification notifications are  
NOT triggered.  These notifications only appear when inserting,  
updating or deleting data AFTER the file has been loaded.

The question of the hour is: how can transients be recomputed from  
data in other entities when the data file is loaded from storage?  It  
appears that the NSManagedObjectContextObjectsDidChangeNotification  
notification is not sent when data is inserted on LOAD.
--
Boisy G. Pitre
337.781.3570 mobile
email: boisy at boisypitre.com
Website: www.boisypitre.com



More information about the MacOSX-dev mailing list