Hello,
I am working a music app with the new MusicKit API and I am not sure how to fix my current warning. I have the following methods to get recently played songs and another used to insert a new song into the queue after the current song. Both methods work but I am receiving the purple warning:
“Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.”
func getRecentlyPlayed(){
let request = MusicRecentlyPlayedRequest()
Task{
let response = try await request.response()
let musicPlayer = ApplicationMusicPlayer.shared
musicPlayer.queue = ApplicationMusicPlayer.Queue(for: response.items)
try await musicPlayer.prepareToPlay()
}
}
func insertNextInQueue(items: MusicItemCollection) {
Task{
let musicPlayer = ApplicationMusicPlayer.shared
try await musicPlayer.queue.insert(items, position: .afterCurrentEntry)
}
}
I have tried everything to force the insert to happen on the main thread but am not able to get the warning to go away. Despite the warning, the insert method works and doesn’t seem to impact my application at all. I am not sure what else to do at this point. Any help or suggestions would be much appreciated.
Thanks,
Dan