如何在核心数据中添加多个关系?

时间:2018-01-26 13:01:30

标签: ios swift core-data relationship

我正在尝试将歌曲添加到多个播放列表中。我在下面使用此代码,但是当我使用此功能时,它会一直覆盖与其他播放列表的旧关系。例如,我将SongA添加到playlist(A),我再次调用此函数并将SongA添加到playlist(B)SongA中的playlist(A)消失了。

open class func addToRelationships(_ identifier : String, playlistName : String){
    print("Adding Relationship to the Song and the Playlist")  
    let selectedPlaylist = getPlaylist(playlistName)
    let selectedSong = getSong(identifier)
    //add song reference to songs relationship (in playlist entity)
    let playlist = selectedPlaylist.mutableSetValue(forKey: "songs")
    playlist.add(selectedSong)
    //add playlist reference to p laylists relationship (in song entity)
    let inPlaylists = selectedSong.mutableSetValue(forKey: "playlists")
    inPlaylists.add(selectedPlaylist) 
    save()
}

我目前正在将此用于核心数据。

Data Model

0 个答案:

没有答案