Skip navigation.
 
mlHow to make KVO's 'addObserver' survive program restart?
FROM : Arthur C.
DATE : Tue Aug 01 19:23:31 2006

(was: How to make KVO setup persistent?)

I have two Core Data entities (say Person, Age), each with 0 instances to
start with. In the program, I create n instances of Person, which creates n
instances of Age. KVO is set up by:

@interface Person
{
  Age * myAge;
}

@implementation Person
- (void) awakeFromInsert
{
// fragment
   [self setAge: [NSEntityDescription insertNewObjectForEntityForName:
@"Age" inManagedObjectContext: managedObjectContext]];

[self addObserver:myAge forKeyPath:@"yearofbirth"
options:NSKeyValueObservingOptionNew context:nil];
}

So far, so good. But when the application is quit and relaunched, the KVO
setup is lost. What I'm trying to achieve is to set it up again in
awakeFromFetch.

I have a solution which is (still) working, and with the most important
memory issues of the previous version fixed.
I now use accessor methods to set/get an instance variable 'myAge' in the
Person object.

-(void) awakeFromFetch
{
// first, fetch the 'Age' object with index equal to [self index] using
NSFetchRequest
// after setting up the fetch request, this is done by:

fetchResults = [managedObjectContext executeFetchRequest: fetchRequest
error: &fetchError];

[self setAge: [fetchResults objectAtIndex:0]]; // contains the right 'Age'
instance.

[self addObserver: [self Age]  forKeyPath:@"yearofbirth" options:
NSKeyValueObservingOptionNew context:nil];
}

Although it works, I still wonder whether it's necessary to go through the
trouble of fetching the 'Age' object myself. Anyway, the 'myAge' defined
above is not available again after program restart.

I hope someone knows a better/simpler solution?


Best regards,

Arthur C.

_________________________________________________________________
MSN Search, for accurate results! http://search.msn.nl

Related mailsAuthorDate
mlRe: How to make KVO setup persistent? Arthur C. Jul 28, 15:13
mlRe: How to make KVO setup persistent? mmalc crawford Jul 28, 16:17
mlRe: How to make KVO setup persistent? Jon Hull Jul 28, 22:54
mlRe: How to make KVO setup persistent? Arthur C. Jul 29, 15:28
mlHow to make KVO's 'addObserver' survive program restart? Arthur C. Aug 1, 19:23