Skip navigation.
 
mlawakeFromUndo?
FROM : Hamish Allan
DATE : Sun May 04 17:39:44 2008

I have a Cocoa app presenting a classic master-detail view of managed
objects, each of which represents a machine on a network and has a
string property "address". I also have a class which monitors the
status of a networked machine whose initialiser takes such an address.
Therefore in awakeFromInsert and awakeFromFetch in my NSManagedObject
subclass I add self as an observer for self's address property, and in
the observeValueForKeyPath:... method I set up a new or replacement
monitoring object. Conversely, in willTurnIntoFault I remove the
monitoring object and the observer.

This works fine as I add and remove items using my array controller,
but if I remove an item and then undo that removal, the object is
reinserted into the context, but without awaking either from insert or
from fetch. I am wondering where I should put my hook for adding the
address observer, or whether I should be doing something different
altogether.

Thanks,
Hamish

P.S. I have written the following workaround, but it relies on a private method:

//  NSManagedObjectContextWorkaround.h

#import <Cocoa/Cocoa.h>

@interface NSManagedObjectContextWorkaround : NSManagedObjectContext
{

}

@end

@interface NSManagedObjectContext (PrivateNSManagedObjectContextWorkaround)

- (void)_undoDeletions:(id)object;

@end

//  NSManagedObjectContextWorkaround.m

#import "NSManagedObjectContextWorkaround.h"

@implementation NSManagedObjectContextWorkaround

- (void)_undoDeletions:(id)deletions
{
   [super _undoDeletions:deletions];
   @try
   {
       for (NSManagedObject *deletion in [deletions objectAtIndex:0])
           [deletion awakeFromFetch]; // treating this as a fetch works for my purposes
   }
   @catch (NSException *exception)
   {

   }
}

@end

Related mailsAuthorDate
No related mails found.