Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make KVO setup persistent?



It's not clear exactly what you're trying to achieve, but the following is certainly not a correct solution for anything.

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:


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

[self addObserver:myAge forKeyPath:@"yearofbirth"
options:NSKeyValueObservingOptionNew context:nil]; // context set to nil works indeed.
}


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.

You've now leaked an instance of Age (you lost the reference to the one you alloced in the first line).

Point taken. But that can be fixed.

It's not clear where fetchResults comes from.

I create an NSFetchRequest after which the results are obtained using:

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

[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.

Line removed, it's not necessary indeed.

[myAgeArray addObject: myAge];

What is myAgeArray? Is it a persistent property? You're modifying it in a non-KVO-compliant way.

This is an attempt to retain the objects fetched using the fetchRequest. But probably not a very good one - I need to work out the memory management issues first.


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

The context here should almost certainly not be the managed object context.

Setting it to 'nil' works as well, as I don't use it in the observeValueForKeyPath method.



Thanks so far,

Arthur C.

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

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: How to make KVO setup persistent? (From: mmalc crawford <email@hidden>)



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

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.