Issue setting a queue with library and non-library items at the same time (plus a couple more MusicKit issues)

As the summer continues, I have been diving deeper and deeper into MusicKit, largely with great results. A few issues have arisen that I've outlined here, feedbacks already filed and numbers included here. All of this happens on the lasted developer beta and latest Xcode beta. Thanks!


FB10967343 - Setting the queue with library and non-library items at the same time doesn't work correctly

In my app, I am working on a feature that lets a user shuffle songs from a collection of albums that may or may not be in their library. However, I’ve discovered an issue where the queue does not seem to work correctly when mixing these types. I’ve attempted to load ApplicationMusicPlayer by creating a Queue and to load applicationQueuePlayer using a MPMusicPlayerPlayParametersQueueDescriptor, but the same issue occurs each time. The queue is able to play songs from the same source, but if it’s been playing a library song and tries to move to a non-library song, the queue stops. 

The first thing I do is pick random songs from each album, using a MusicLibraryRequest or a MusicCatalogResourceRequest as appropriate, then taking a randomElement() from the ensuing MusicItemCollection for the album.  I append each track to an array, which I then cast to MusicItemCollection so I’ve now got a MusicItemCollection consisting of the tracks I want.

If I’m in MusicKit land, I simply set the queue as follows:

 player.queue = ApplicationMusicPlayer.Queue(for: tracks)

It takes a bit more doing in MediaPlayer, but in theory this should also work, right?

   do {

        let paramObjects = tracks.compactMap {
            $0.playParameters
        }
       
     let params = try paramObjects.map({try JSONEncoder().encode($0)})

    let dicts = try params.compactMap {

              try JSONSerialization.jsonObject(with: $0, options: []) as? [String:Any]
           }

          let finalParams = dicts.compactMap {

                MPMusicPlayerPlayParameters(dictionary: $0)

            }

 let descriptor = MPMusicPlayerPlayParametersQueueDescriptor(playParametersQueue: finalParams)
	mediaPlayer.setQueue(with: descriptor)

} catch {

print(error)

}


In either case, the following issue occurs: say that I end up with a queue made up of one library song, then one non-library song. The player will play just the first song, then it acts as if the queue has ended. Say that it has two non-library songs, then one library song. Just the two non-library songs play. Indeed, printing queue.entries shows just the number of items that were from the same source type.


FB10967076 - Publishing changes from background thread error when inserting queue items

When using the .insert method on ApplicationMusicPlayer.Queue on the last iOS 16 and Xcode betas, it returns a “Publishing changes from background thread” error even though the function I’m doing in is marked as a @MainActor and the stacktace indicates it was on the main thread.


FB10967277 - song.with([.albums], preferredSource: .library) generates thousands of lines of EntityQueries in the console

I’ve noticed that when using the preferredSource: .library when requesting additional properties on a library item creates ~6,000 of “EntityQuery” entries in the console, all in the span of a second. This doesn’t seem to be leading to any major performance issues, but it sure seems like something isn't right.

 let request = MusicLibraryRequest<Song>.init()
        
        do {
            let response = try await request.response()
            
            guard let song = response.items.first else {
                return
            }
            let songWithAlbums = try await song.with([.albums], preferredSource: .library)
            
        } catch {
            print(error)
        }

generates the following output (except... 6,000 of them)

2022-07-31 13:02:07.729003-0400 MusicKitFutzing[9405:2192606] [EntityQuery] Finished fetching results in 0s
2022-07-31 13:02:07.729047-0400 MusicKitFutzing[9405:2192605] [EntityQuery] Finished executing query in 0.00100017s
2022-07-31 13:02:07.729202-0400 MusicKitFutzing[9405:2192611] [EntityQuery] Finished executing query in 0s
2022-07-31 13:02:07.729240-0400 MusicKitFutzing[9405:2192605] [EntityQuery] Finished fetching results in 0s