我正在尝试将歌曲添加到多个播放列表中。我在下面使用此代码,但是当我使用此功能时,它会一直覆盖与其他播放列表的旧关系。例如,我将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()
}
我目前正在将此用于核心数据。