Fix condition race in deleting element and insert new one

This commit is contained in:
CappielloAntonio 2021-04-18 19:26:54 +02:00
parent 3addc3b561
commit fc430e5811
3 changed files with 24 additions and 10 deletions

View file

@ -11,6 +11,7 @@ import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.model.AlbumArtistCross;
import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.model.SongArtistCross;
import com.cappielloantonio.play.util.QueueUtil;
import java.util.ArrayList;
@ -27,9 +28,17 @@ public class AlbumArtistRepository {
}
public void insertAll(List<AlbumArtistCross> crosses) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumArtistCrossDao, crosses);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread delete = new Thread(new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(albumArtistCrossDao, crosses));
delete.start();
delete.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static class InsertAllThreadSafe implements Runnable {

View file

@ -7,6 +7,7 @@ import com.cappielloantonio.play.database.dao.AlbumArtistCrossDao;
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.AlbumArtistCross;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.model.SongArtistCross;
import java.util.List;
@ -22,9 +23,17 @@ public class SongArtistRepository {
}
public void insertAll(List<SongArtistCross> crosses) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songArtistCrossDao, crosses);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread delete = new Thread(new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songArtistCrossDao, crosses));
delete.start();
delete.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static class InsertAllThreadSafe implements Runnable {

View file

@ -271,8 +271,6 @@ public class SyncFragment extends Fragment {
* Sincronizzazzione dell'album con gli artisti che hanno collaborato per la sua produzione | isProduced = false
*/
private void syncAlbumArtistCross(ArrayList<Album> albums) {
albumArtistRepository.deleteAll();
List<AlbumArtistCross> crosses = new ArrayList<>();
for(Album album: albums) {
@ -298,8 +296,6 @@ public class SyncFragment extends Fragment {
}
private void syncSongArtistCross(ArrayList<Song> songs) {
songArtistRepository.deleteAll();
List<SongArtistCross> crosses = new ArrayList<>();
for(Song song: songs) {