Editing a Library Playlist (MusicKit: iOS 16 beta)

I've just begun to dip my toes into the iOS16 waters.

One of the first things that I've attempted is to edit a library playlist using:

try await MusicLibrary.shared.edit(targetPlaylist, items: tracksToAdd)

Where targetPlaylist is of type MusicItemCollection<MusicKit.Playlist>.Element and tracksToAdd is of type [Track]

The targetPlaylist was created, using new iOS16 way, here:

let newPlaylist = try await MusicLibrary.shared.createPlaylist(name: name, description: description)

tracksToAdd is derived by performing a MusicLibraryRequest on a specific playlist ID, and then doing something like this:

if let tracksToAdd = try await playlist.with(.tracks).tracks {
   // add tracks to target playlist
}

My problem is that when I perform attempt the edit, I am faced with a rather sad looking crash.

libdispatch.dylib`dispatch_group_leave.cold.1:
    0x10b43d62c <+0>:  mov    x8, #0x0
    0x10b43d630 <+4>:  stp    x20, x21, [sp, #-0x10]!
    0x10b43d634 <+8>:  adrp   x20, 6
    0x10b43d638 <+12>: add    x20, x20, #0xfbf          ; "BUG IN CLIENT OF LIBDISPATCH: Unbalanced call to dispatch_group_leave()"
    0x10b43d63c <+16>: adrp   x21, 40
    0x10b43d640 <+20>: add    x21, x21, #0x260          ; gCRAnnotations
    0x10b43d644 <+24>: str    x20, [x21, #0x8]
    0x10b43d648 <+28>: str    x8, [x21, #0x38]
    0x10b43d64c <+32>: ldp    x20, x21, [sp], #0x10
->  0x10b43d650 <+36>: brk    #0x1

I assume that I must be doing something wrong, but I frankly have no idea how to troubleshoot this.

Any help would be most appreciated. Thanks. @david-apple?

Hi Kimfucious,

Thank you for your feedback! I think the best way to help you out is for you to file a bug report Feedback Assistant. Thanks again!

-David

Thanks @david-apple,

I've raised a ticket (FB10328182) on Feedback Assistant as you've suggested.

As a follow up, the following does work:

     for track in tracksToAdd {
          try await MusicLibrary.shared.add(track, to: targetPlaylist)
     }

However, this is adding, not editing.

The thing is that I’m really interested in the edit functionality, so as to be able to remove tracks.

Hi @JoeKun & @david-apple,

I have found that if I retrieve the items by using preferredSource: .catalog in the .with, per the below, that the MusicLibrary edit does not crash.

excluding preferredSource or using preferredSource: .library in the .with will cause the crash.

if let tracksToAdd = try await playlist.with(.tracks, preferredSource: .catalog).tracks {
   // add tracks to target playlist
}

I've updated the ticket accordingly.

Also, as a side note, it seems that .with only returns a max of 100 tracks. I'll keep testing and raise under separate cover, if I can't figure that out.

Hi @Kimfucious,

I am also trying to work with this edit playlist API. When using it, all scenarios end up with all the songs in any one of my playlists being deleted. Are you running into this issue? If not, could you explain slightly more in-depth how you got the API working? When you mentioned using preferredSource: .catalogand it not crashing, did the edit work?

Hi @CPDigitalDarkroom,

The trick to using .edit, is that you need provide an array (i.e. sequence) of .items.

In the below, playlist is the actual playlist retrieved using MusicLibraryRequest, and items is an array/sequence of tracks.

With .edit, items replaces all tracks in the playlist, so you need to be sure you're providing what you want to be in the playlist with items.

let updatedPlaylist = try await MusicLibrary.shared.edit(playlist, items: items)

If all tracks are being removed from your playlist, using .edit, I would check to make sure that you are supplying and actual array of items. If that's empty, then you'll wind up with an empty playlist.

I hope that makes sense. If not, feel free to ask, and I'll do my best to help you out.

@JoeKun

This bug is back again.

Long story, short: I redeployed an app that hasn't changed code-wise.

This line causes stops the function that it's in and throws no error. It also does not edit the playlist.

try await MusicLibrary.shared.edit(targetPlaylist, items: items)

The only things that have changed, I imagine, are related to new releases of software since WWDC 2023.

HI @Kimfucious,

Did you manage to edit the playlist after WWDC 2023? I am getting the error The operation couldn't be completed. (MPModelLibrarySDKPlaylistEditChangeRequestOperationErrorDomain error -10004.) if you wrap the code in do catch

I also noticed that if you create a playlist let newPlaylist = try await MusicLibrary.shared.createPlaylist(name: "name", items: items)

and then immediately edit try await MusicLibrary.shared.edit(playlist, items: filteredItems)

then everything works fine

I also noticed that the id we get after creating the playlist is different from the id we get with MusicLibraryRequest

HI @Kimfucious,

Did you manage to edit the playlist after WWDC 2023? I am getting the error The operation couldn't be completed. (MPModelLibrarySDKPlaylistEditChangeRequestOperationErrorDomain error -10004.) if you wrap the code in do catch

I also noticed that if you create a playlist

let newPlaylist = try await MusicLibrary.shared.createPlaylist(name: "name", items: items)

and then immediately edit

try await MusicLibrary.shared.edit(playlist, items: filteredItems)

then everything works fine

I also noticed that the id we get after creating the playlist is different from the id we get with MusicLibraryRequest

Hi @kokomola,

What I've written prior still applies.

I will add that sometimes, for whatever reason this stops stops working and then starts working again after an undetermined amount of time.

I have no explanation for why it sometimes doesn't work.

Editing a Library Playlist (MusicKit: iOS 16 beta)
 
 
Q