views:

1017

answers:

1

Hi, I am a cocoa newbie trying to create an iPhone app including Core Data.

My problem is this: I have a little app running now with a single entity called Playlist that I display in a table view and can add and delete entries in.

I have these in my PlayerAppDelegate:

playlistManagedObjectModel

playlistListManagedObjectContext

playlistListPersistentStoreCoordinator

Adding entity with:

Playlist *playlist = (Playlist *)[NSEntityDescription insertNewObjectForEntityForName:@"Playlist" inManagedObjectContext:playlistListManagedObjectContext];

Now I want to add a sublevel called Song with a to-many relation.

Playlist attribute added: songRelation Song attribute added: playlistRelation

I have created this entity and set up the relations both ways, clicking of the Optional flag as I want to have at least one Song in a Playlist.

After setting this relation I can now no longer create a Playlist without getting a warning. The problem is that "it" wants a Song created too, but I dont know how.

I cant find a single place with an example on how to add a new playlist in this case, ie when is has a relation to another entity that has to be added too.

Do I need to create these:

songManagedObjectModel songListManagedObjectContext songListPersistentStoreCoordinator

or is the Song entity accessible via the playlist entity somehow?

Something like this perhaps:

Add Playlist

Add Song

Set up "relation" attributes (how?)

Save to persistent store

Or????

I have really googled a lot and has probably misunderstood something basic here since there are no examples available....

Rgds PM

+4  A: 

Petter,

There should be a method on the PlayList model object that is something like:

- (void) addSongObject:(Song *)value;

It is a dynamically generated method that will insert a Song value into the relationship for songs in PlayList. If you have not generated the PlayList model object since adding the Song relationship, that method will not be there. So, make sure you generate model classes for any changed entities.

The Song object that you pass to the addSongObject method should be created using the ManagedObjectContext like:

Song *song = (Song *)[NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:playlistListManagedObjectContext];

Jack

Jack Cox
Thanks Jack, will try!Not sure how to generate new code from the enity defintions though...but now I know what to look for.So basically I have to start with the creating a Playlist and then add a Song? I cant go the other way around and create a Song, then create a playlist? (I will have much more complex relations later)If you have a complete sequence example I would love it! Something like this since I seem to have a problem with the order of doing this:1. Add Playlist: code...2. Save Playlist: code...3. Add Song: code...4. Save Song: code...5. Connect them: code...Rgds Petter
Petter Magnusson
It worked! File New... got me new .m and .h files. Then I used insertNewObjectForEntityForName to make a Playlist the set its attributes, then insertNewObjectForEntityForName again to make a Song, the set ints attributes, then used addSong, then saved the context. Had not realised that all the entities go into the SAME Model and Context, and that I have to add the Song to the Playlist and not the other way around. So, now I can move on. Thanks again! PS there is really no code or tutorial on this at all on the web (hint...;-)! Rgds PM
Petter Magnusson
Petter,I don't have any code that I can release due to client confidentiality. I agree that a tutorial on this would be good thing to do, but I don't have a tremendous amount of time to do one. What I have learned from using CoreData is that you need to use the insertNewObjectForEntityForName to make new empty empties and use the fetch routines to pull existing entities from the persistence manager. Then use the addXObject to manage the relationships.
Jack Cox
Yes Jack, that is exactly the "missing info" about what is needed to manage relations in Core Data! Perhaps some other reader of this can do a tutorial or sum it up in a good example at least (I have the same time problem as you...;-) Rgds PM
Petter Magnusson