views:

2807

answers:

3

I have an application written using Core Data. I have 2 entities with a one-to-many relationship. I have subclassed NSManagedObject for both of them. The entity on the one-side is called Playlist and the other is called Song.

The interface for Playlist:

@interface VBPlaylist : NSManagedObject {
}

@property (readwrite, copy) NSString *name;

@end

The implementation for Playlist:

@implementation VBPlaylist

@dynamic name;

@end

I think that I should have another property to indicate the Songs under the Playlist class. I cannot find any sample code that shows to-many relationships written as properties. How do you do this?

+11  A: 

To-one relationships are modeled as object references by Core Data. So a to-one relationship from Entity Bar to entity Baz (assuming Baz is implemented by the class Baz) would be

@interface Bar : NSManagedObject {
}

@property (readwrite,nonatomic) Baz * toBaz;

@end

To-many relationships are modeled as a mutable set property (though not as an NSMutableSet). Assuming a to-many relationship from Bar to Baz called manyBazz:

@interface Bar : NSManagedObject {

}

@property (retain) NSSet* manyBazz;

@end

// coalesce these into one @interface AnalysisRecord (CoreDataGeneratedAccessors) section
@interface Bar (CoreDataGeneratedAccessors)
- (void)addManyBazzObject:(Baz *)value;
- (void)removeManyBazzObject:(Baz *)value;
- (void)addManyBazz:(NSSet *)value;
- (void)removeManyBazz:(NSSet *)value;

@end

If you want to use the NSMutableSet interface to manipulate the manyBazz relationship, you should call -mutableSetValueForKey:@"manyBazz" to get a KVO-compliant proxy for the manyBazz relationship.

On Leopard (OS X 10.5) and later, all appropriate methods are automaticall generated at run-time by the Core Data framework, even if you do not explicitly declare or implement them (you will, of course, get a compiler warning if you try to use them without declaring them in a header file). Thus you do not need to subclass

The easiest way to get the declaration and implementation right is to select the attributes in the data modeler and choose "Copy Objective-C 2.0 Method Declarations To Clipboard" from the "Design->Data Model" menu, the paste into your implementing classes .h file. Of course, you have to keep your .h and model in sync... hence a hearty recommendation for rentzsch's awesome MO Generator, a tool that will automatically generate (and re-generate) NSManagedObject subclasses from your data model.

Barry Wark
So in this case if I want to add a Baz object to Bar I can do:[barInstance addManyBazzObject:bazInstance] or bazInstance.bar=barInstance?
hekevintran
Yes, that is correct.
Barry Wark
I think you forgot to finish that last sentence.
Peter Hosey
Thanks, Peter. Oops. Should be fixed now.
Barry Wark
+11  A: 
Jim Dovey
Cool. The generated code makes for good examples for how to set this up. I have been using it a lot today.
Tony Eichelberger
A: 

Also, have a look at this blog post for extremely easy automatic generation of the classes.

Vincent Driessen