fix database concurrency issue

This commit is contained in:
Jakob Kukla 2022-05-16 23:36:48 +02:00 committed by jakobkukla
commit bfd3818c0a
2 changed files with 12 additions and 8 deletions

View file

@ -45,4 +45,15 @@ public abstract class QueueSongDao {
insertQueueSongs(queueSongs);
}
@Transaction
public void updateQueues(List<Song> playingQueue, List<Song> shuffledQueue) {
// copy queues by value to avoid concurrent modification exceptions from database
App.getDatabase().songDao().deleteSongs();
App.getDatabase().songDao().insertSongs(new ArrayList<>(playingQueue));
deleteQueueSongs();
setQueue(new ArrayList<>(playingQueue), 0);
setQueue(new ArrayList<>(shuffledQueue), 1);
}
}

View file

@ -294,14 +294,7 @@ public class QueueManager {
public void saveQueue() {
PreferenceUtil.getInstance(context).setPosition(position);
// copy queues by value to avoid concurrent modification exceptions from database
App.getDatabase().songDao().deleteSongs();
App.getDatabase().songDao().insertSongs(new ArrayList<>(playingQueue));
App.getDatabase().queueSongDao().deleteQueueSongs();
App.getDatabase().queueSongDao().setQueue(new ArrayList<>(playingQueue), 0);
App.getDatabase().queueSongDao().setQueue(new ArrayList<>(shuffledQueue), 1);
App.getDatabase().queueSongDao().updateQueues(playingQueue, shuffledQueue);
}
public int getRestoredProgress() {