• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
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?


  • Subject: Re: How to make KVO setup persistent?
  • From: "Arthur C." <email@hidden>
  • Date: Fri, 28 Jul 2006 15:13:11 +0200

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


  • Follow-Ups:
    • Re: How to make KVO setup persistent?
      • From: Jon Hull <email@hidden>
    • Re: How to make KVO setup persistent?
      • From: mmalc crawford <email@hidden>
  • Prev by Date: Re: NSTableView - Programmatically creating Table Columns and populating
  • Next by Date: Frameworks + sharedFrameworksPath
  • Previous by thread: Re: How to make KVO setup persistent?
  • Next by thread: Re: How to make KVO setup persistent?
  • Index(es):
    • Date
    • Thread