[baseten-users] adding observers in BXDatabaseObject subclasses for when a related record is deleted/modified

Kevin Palser kpalser at sonrie.com
Thu Mar 18 19:34:43 EET 2010


Hi,

I have a question which is connected to Réjean Lamy series of queries in May 2009.

Like Réjan, I recreated the Department / Employee example in Aaron Hillegass book with a notable exception. I do not know how to code the subclass BXDatabaseObject for the department data. In the Hillegass example this subclass has one simple function:

"When an employee who is the manager leaves the department, you will need to set the manager to nil"

The example from the book uses Core Data generated accessors for the Department BXDatabaseObject subclass. I am unsure if there is a direct equivalent for these accessors in BaseTen, so I placed an observer its implementation:

- (id) init {
   [self addObserver: self forKeyPath: @"employees" options: 0  context: nil];
   return self;
}

- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {
   if ( … ) {
	// Handle the change by checking if the changed employee record is the manager
   }
   else
       [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
}

- (void)dealloc {
   [self removeObserver:self forKeyPath:@"employees"];
   [super dealloc];
}

"employees" (or "EmployeeSet") is the relationship between the tables Department and Employee. However, in the above code observeValueForKeyPath is only called when an employee is associated with a department. It is not called when a related employee record is deleted.

Could you set me on the right path with an example code snippet?

Thanks,
Kevin Palser




More information about the baseten-users mailing list