small medium large xlarge

Back to: All Forums  Core Data
15 Jun 2010, 00:37
Jorge D. Ortiz Fuentes (2 posts)

Dear Marcus,

Congratulations on your book. A very useful one indeed.

In page 40 of your book, you suggest to write accessors for relationships in order to be able to use NSTreeController that requires a the same property name for all the hierarchy. I think that this is a great idea, but I am not sure if my implementation is right. Let me use Aperture’s data model as an example: You can have many libraries, each one of them can have many projects, and each of them can have many photos. So, if I name my Entities Library, Project, and Photo and their relationships projects, photos and nothing respectively is the following a proper implementation for Library:


@interface Library: NSManagedObject {

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSSet *projects;
@property (nonatomic, retain) NSSet *children;

@interface Library (CoreDataGeneratedAccessors)
- (void)addProjectsObject:(Project *)value;
- (void)removeProjectsObject:(Project *)value;
- (void)addProjects:(NSSet *)value;
- (void)removeProjects:(NSSet *)value;


and Library.m

#include "Library.h"

@implementation Library

@dynamic title;
@dynamic projects;

- (NSSet *) children {
	return [self projects];

- (void) setChildren(NSSet *)children {
	[self setProjects:children];

  1. Is this the suggested implementation? Should I also include addChildrenObject:, removeChildrenObject:, addChildren:, and removeChildren:? And implement them?

  2. I assume that children doesn’t appear at all in the Core Data model. Is that right?

  3. Should I call [self willChangeValueForKey:@"children"] in setChildren: so children is KVO compliant?

  4. In page 41 you recommend to implement NSOutlineDataSource instead of using NSTreeController due to “results [that] can be unexpected and unclear”. Could you please elaborate on that?

  5. Finally, if I implement NSOutlineDataSource, would you recommend caching the fetch for the root objects? And if so, what is the proper way to maintain this cached array in sync with Core Data?

Thank you.

Best regards,


You must be logged in to comment