Re: How to make KVO setup persistent?
Re: How to make KVO setup persistent?
- Subject: Re: How to make KVO setup persistent?
- From: mmalc crawford <email@hidden>
- Date: Fri, 28 Jul 2006 07:17:05 -0700
On Jul 28, 2006, at 6:13 AM, Arthur C. wrote:
Finally I have a working solution. It sets up KVO again after
application restart. It is as follows:
It's not clear exactly what you're trying to achieve, but the
following is certainly not a correct solution for anything.
-(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
You've now leaked an instance of Age (you lost the reference to the
one you alloced in the first line).
It's not clear where fetchResults comes from.
[myAge index]; // to get rid of the 'fault' state (!)
It's not clear why you need to "get rid of the fault state" -- fault
handling should typically be transparent.
[myAgeArray addObject: myAge];
What is myAgeArray? Is it a persistent property? You're modifying it
in a non-KVO-compliant way.
[self addObserver: [myAgeArray lastObject]
forKeyPath:@"yearofbirth" options: NSKeyValueObservingOptionNew
context:managedObjectContext];
The context here should almost certainly not be the managed object
context.
[myAgeArray dealloc];
You should never invoke another object's dealloc method directly. See
the memory management documentation...
The point is that the fetched 'Age' needs to be retained, or else it
will get autoreleased and dealloced.
... yes, that's expected behaviour...
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."
... which simply means that you must follow normal memory management
rules. Observing something doesn't mean it won't go away unless
you've taken ownership of it.
You should review at least <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/ObjectOwnership.html
>.
mmalc
_______________________________________________
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