Array Function of MPMediaItem Very Slow

149 Views Asked by At

I'm trying to edit the queue of my music player using the applicationQueuePlayer and the perform method (details here). However, whenever I apply any array function (map, filter etc.), it takes many seconds to complete, leading to (I think) data races and crashes when the user, for example, removes two tracks immediately after each other.

    var musicPlayerController = MPMusicPlayerController.applicationQueuePlayer

    self.musicPlayerController.perform { (currentQueue) in
        let items = currentQueue.items
        let itemsToRemove = items.filter { $0.artist == "Some artist" } // this takes multiple seconds
        if let item = itemsToRemove.first {
            currentQueue.remove(item)
        }
    } completionHandler: { (newQueue, error) in
        if let e = error {
            print(e)
        } else {
            tracks = items.map { Track(item: $0) } // this takes multiple seconds
        }
    }

The issue is arising as I'm going through an MPMediaItem array. I don't think this is an issue with the MPMediaItem class though, as I'm able to complete a map of [MPMediaItem] in other places in the app e.g. when getting items from a playlist (a similar sized array to the queue items).

The issue happens solely when the MPMediaItems are taken from the MPMusicPlayerControllerMutableQueue and MPMusicPlayerControllerQueue

Is this just a bug with MusicKit API?

0

There are 0 best solutions below