Albums added to playlist are not always ordered correctly
An album added to some playlist may not always be in the order of its track list.
Expected behaviour
An album added to a playlist should appear ordered as its track list.
Current behaviour
Sometimes an album does not get appended to the playlist in the order of its track list.
Looking at add_songs we can see how this on seemingly rare occasions it can occur because we use async queries and the order in which the callbacks are called cannot be guaranteed. carlosg pointed that
...all update() calls are processed sequentially in the update thread in the same order they are received. But IIRC g_task_return*() may either schedule an idle callback on the caller thread, or simply acquire its main context and execute the callback in place
Steps to Reproduce
- In the artists view choose some artist's album with tracks > 10 (more tracks more queries therefore higher chances of issue occurring)
- Click the three dot menu and select add to playlist
- Add to a new playlist
- Switch to playlist view and select the new playlist(it is only now that the model is populated, hence the initial loading)
- Switch back to artists view and redo steps 2 adding to playlist from step 3
- wait a bit(the model is populating) before switching back to playlists view
- open some view of the album to compare with your playlist, should find that some tracks are not where they should be
This may require a couple tries, for me it hasn't been more than 3 before I can reproduce
Possible Solution
One idea is to have the songs added in a sort of sync fashion while still using the async functions. This at least ensures we still append to the model(which is desired visually as songs won't be shuffled to place) in this case the _add_to_model function triggers the insert of the next song when it's done so we have this order of action: insert->_requery_media->_add_to_model->insert->_requery_media->... suggested by carlosg.
mschraal believes the _requery_media step can be skipped, which may lessen the likely hood of the issue.