Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: How to make KVO setup persistent?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make KVO setup persistent?



Finally I have a working solution. It sets up KVO again after application restart. It is as follows:

@implementation Person
NSMutableArray *myAgeArray;

+(void) initialize
{
myAgeArray = [[NSMutableArray arrayWithCapacity: 5] retain]; // or any number
}


-(void) awakeFromFetch
{
// first, fetch the 'Age' object with index equal to [self index] using NSFetchRequest


Age * myAge = [[Age alloc] init];
myAge = [fetchResults objectAtIndex: 0]; // only one object gets fetched
[myAge index]; // to get rid of the 'fault' state (!)
[myAgeArray addObject: myAge];


[self addObserver: [myAgeArray lastObject] forKeyPath:@"yearofbirth" options: NSKeyValueObservingOptionNew context:managedObjectContext];
}


-(void) dealloc
{
 [myAgeArray dealloc];
 [super dealloc];
}

The point is that the fetched 'Age' needs to be retained, or else it will get autoreleased and dealloced. The global array takes care of that. The solution appears to work OK, but maybe there is a more efficient way?

I believe this problem is covered in the KVO programming Guide, where it (only) says:
"Note: The key-value observing addObserver:forKeyPath:options:context method does not retain the observing object or the observed objects. You need to review your application's requirements and manage retain and release for the observing, and observed objects."


This looks not particularly enlightening to me, so I will file a documentation enhancement request (in particular a piece of sample code would do well).


Thanks for your time,

Arthur C.

_________________________________________________________________
Hotmail en Messenger on the move http://www.msn.nl/services

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.