MPMediaQuery for persistentID fails immediately after MPMediaPickerController returns item

I have a production app that I recently added support for playing Apple Music songs.


The user can pick Apple Music songs via MPMediaPickerController, I save the persistentID and then use an MPMediaQuery to retrieve the MPMediaItem and play it with the MPMusicPlayerController.


This logic works 95% of the time, but in the remaining opportunities, the MPMediaQuery returns no items matching the persistentID that was just provided by the MPMediaPickerController.


I have not been able to duplicate the problem, but have implemented extensive error logging to debug the issue and made the following observiations.


This only happens with Apple Music songs indicated by "hasProtectedAsset" flag set.

The MPMediaItems do not have the "isCloudItem" set.

This seems to happen with multiple songs from the same users.

It has occured with devices from iPhone 6 up to iPhone X

I have only observed this behavior on devices with iOS 11.2.6

The users SKCloudServiceCapabilityMusicCatalogPlayback capability is verified.

The persistentID that is returned with the MPMediaItem from the MPMediaPickerController is verified the same as the one used in the query.

The query failure can occur within seconds of the MPMediaPickerController return so it is unlikely anything has happened to the device or music library.

The query will fail again if retried.

A subsequent MPMediaQuery using a predicate of the artist and track name will also fail.

The user can go back into their library in the music app, remove and readd the item, it will get the same persistentID and the MPMediaQuery will no longer fail.


Is this an indicator of some sort of corruption in the users media library?

Is there anything I can do to detect this condition and/or advise the user how to resolve it?

>The query failure can occur within seconds of the MPMediaPickerController return so it is unlikely anything has happened to the device or music library.


Including a sync?

I did eventually find the solution to this issue. Turns out the users were picking songs from playlists but the individual songs had not been added to their library. Using a playlist query instead of a songs query would find the item by persistent ID. Now, of course, with iOS 14 those items selected with the picker from playlists that do not exist in the library have a '0' value persistent Id. Another iOS update, another SMH moment. In theory, I could add the items with the '0' persistentID to the MPMediaLibrary with the playbackStoreID, but that function is broken and hangs in every iOS 14.x.x version up to 14.4.1 so far. Sigh...
@teststation5 check out my last message here https://developer.apple.com/forums/thread/658557?page=2 (not the accepted answer because I'm dumb, didn't know what that button did). Long story short, you gotta wrap the addToLibrary func in a dispatch that's not for the main queue.
MPMediaQuery for persistentID fails immediately after MPMediaPickerController returns item
 
 
Q