I am using core data. I am having 2 Entities Music and Lyrics .I created relationship musicTolyrics with inverse and To-many. I have to save number of timestamps and lyrics for one song. I m using this code
Lyrics *lyrics = [NSEntityDescription insertNewObjectForEntityForName:@"Lyrics"
inManagedObjectContext:context];
lyrics.Lyrics = albumTitleField.text;
lyrics.startTime = 0.0;
lyrics.lyricsTomusic = music;
开发者_JS百科
This is saving this in coredata. but I want to save number of records for 1 song..just like Foreign key.. How can I do this ? please help.
Thanks in advance
It looks like your inverse is the wrong way. Rather than "Music" which is quite ambiguous, let's call it "Song". So, a Song
has "Lyrics". Other Songs could also use the same Lyrics
.
So far then, we have a many-to-one relationship in that Many Songs can use the same Lyrics.
What I'd recommend is that you map this out in the Core Data modeller. Then, generate the class files automatically. You'll then see that in the to-many relationship, you can add an object to a set, so you'll have for example:
Song *mySong = [NSEntityDescription insertNewObjectForEntityForName:@"Song"
innManagedObjectContext:context];
Lyrics *myLyrics = [NSEntityDescription insertNewObjectForEntityForName:@"Lyrics"
innManagedObjectContext:context];
That's the two objects in Core Data. Now to define the relationship with the inverse:
Song.lyrics = myLyrics;
[myLyrics addSongObject:mySong];
If you set things up correctly in the model, you'll see that method addSongObject
generated for you automatically.
精彩评论