iphone - Core data with to-many relationship/ -
i using core data. having 2 entities music , lyrics .i created relationship musictolyrics inverse , to-many. have save number of timestamps , lyrics 1 song. m using code
lyrics *lyrics = [nsentitydescription insertnewobjectforentityforname:@"lyrics" inmanagedobjectcontext:context]; lyrics.lyrics = albumtitlefield.text; lyrics.starttime = 0.0; lyrics.lyricstomusic = music; this saving in coredata. want save number of records 1 song..just foreign key.. how can ? please help.
thanks in advance
it looks inverse wrong way. rather "music" quite ambiguous, let's call "song". so, song has "lyrics". other songs use same lyrics.
so far then, have many-to-one relationship in many songs can use same lyrics.
what i'd recommend map out in core data modeller. then, generate class files automatically. you'll see in to-many relationship, can add object set, you'll have example:
song *mysong = [nsentitydescription insertnewobjectforentityforname:@"song" innmanagedobjectcontext:context]; lyrics *mylyrics = [nsentitydescription insertnewobjectforentityforname:@"lyrics" innmanagedobjectcontext:context]; that's 2 objects in core data. define relationship inverse:
song.lyrics = mylyrics; [mylyrics addsongobject:mysong]; if set things correctly in model, you'll see method addsongobject generated automatically.
Comments
Post a Comment