MusicLibrary.shared.createPlaylist adds wrong track to playlist

I’ve been trying to debug an issue where tracks in playlists created using the new MusicLibrary.shared.createPlaylist function seem to be missing their artwork. For example:

I’ve found that when I look at the track which MusicKit added in the above example, it’s not the one which I specified - it’s a different version (catalogId) of the same song. Interestingly it also seems to be marked as a library track (the track is definitely not in my library). Here is the code:

let request = MusicCatalogResourceRequest<Song>(matching: \.id, equalTo: MusicItemID("1440800296"))
let response = try await request.response()
let initialCatalogId = response.items.first?.id.rawValue ?? "?"

let playlist = try await MusicLibrary.shared.createPlaylist(name: "My Playlist", description: nil, authorDisplayName: "My App", items: response.items)

while true {
    guard let entry = try? await playlist.with([.entries], preferredSource: .library).entries?.first else {
        continue
    }

    // This is just for debug purposes to work out what track actually got added:
    let data = try JSONEncoder().encode(entry.playParameters)
    guard let root = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
        continue
    }

    let newCatalogId = root["catalogId"] as? String ?? "?"
    let isLibrary = root["isLibrary"] as? Bool ?? false
    print(initialCatalogId, newCatalogId, isLibrary)
    // Prints: 1440800296 309186793 true

    break
}

If I’m doing something wrong either when saving the playlist or retrieving the catalogId of the track which was saved please let me know: I confess I’m finding it quite hard to navigate MusicKit Swift at the moment as examples & documentation seem quite thin on the ground.

However if the above behaviour is the best I can expect then it’s a serious issue for me - the catalogId of the track I want to add, above, was carefully selected so if MusicKit is going to randomly swap this out then I can’t use it. I’ve created FB11589088 for this issue.

@JoeKun@david-apple: I’ve raised a number of issues with MusicKit on here recently and have received no responses at all. Appreciate you guys are really busy but I think a little help here would go a long way to making MusicKit the success it deserves to be. Please let me know if I should be doing this differently.

Thanks!

MusicLibrary.shared.createPlaylist adds wrong track to playlist
 
 
Q