Changed the order of deletion and saving of items at the time of synchronization

This commit is contained in:
CappielloAntonio 2021-05-02 12:20:43 +02:00
parent 01785b7bed
commit 86fcc8b479
13 changed files with 138 additions and 51 deletions

View file

@ -20,11 +20,11 @@ public class AlbumArtistRepository {
public void insertAll(List<AlbumArtistCross> crosses) {
try {
final Thread delete = new Thread(new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao));
final Thread deleteAll = new Thread(new DeleteAllAlbumArtistCrossThreadSafe(albumArtistCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(albumArtistCrossDao, crosses));
delete.start();
delete.join();
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {

View file

@ -64,9 +64,17 @@ public class AlbumRepository {
}
public void insertAll(ArrayList<Album> albums) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(albumDao, albums);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread deleteAll = new Thread(new DeleteAllThreadSafe(albumDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(albumDao, albums));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
@ -158,7 +166,6 @@ public class AlbumRepository {
@Override
public void run() {
albumDao.deleteAll();
albumDao.insertAll(albums);
}
}

View file

@ -56,9 +56,17 @@ public class ArtistRepository {
}
public void insertAll(ArrayList<Artist> artists) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(artistDao, artists);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread deleteAll = new Thread(new DeleteAllThreadSafe(artistDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(artistDao, artists));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
@ -117,7 +125,6 @@ public class ArtistRepository {
@Override
public void run() {
artistDao.deleteAll();
artistDao.insertAll(artists);
}
}

View file

@ -26,9 +26,17 @@ public class PlaylistRepository {
}
public void insertAll(ArrayList<Playlist> playlists) {
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(playlistDao, playlists);
Thread thread = new Thread(insertAll);
thread.start();
try {
final Thread deleteAll = new Thread(new DeleteAllThreadSafe(playlistDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(playlistDao, playlists));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
@ -65,7 +73,6 @@ public class PlaylistRepository {
@Override
public void run() {
playlistDao.deleteAll();
playlistDao.insertAll(playlists);
}
}

View file

@ -24,12 +24,6 @@ public class PlaylistSongRepository {
thread.start();
}
public void deleteAll() {
DeleteAllPlaylistSongCrossThreadSafe delete = new DeleteAllPlaylistSongCrossThreadSafe(playlistSongCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private PlaylistSongCrossDao playlistSongCrossDao;
private List<PlaylistSongCross> crosses;
@ -45,6 +39,12 @@ public class PlaylistSongRepository {
}
}
public void deleteAll() {
DeleteAllPlaylistSongCrossThreadSafe delete = new DeleteAllPlaylistSongCrossThreadSafe(playlistSongCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class DeleteAllPlaylistSongCrossThreadSafe implements Runnable {
private PlaylistSongCrossDao playlistSongCrossDao;

View file

@ -20,11 +20,11 @@ public class SongArtistRepository {
public void insertAll(List<SongArtistCross> crosses) {
try {
final Thread delete = new Thread(new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao));
final Thread deleteAll = new Thread(new DeleteAllSongArtistCrossThreadSafe(songArtistCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songArtistCrossDao, crosses));
delete.start();
delete.join();
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {

View file

@ -0,0 +1,70 @@
package com.cappielloantonio.play.repository;
import android.app.Application;
import com.cappielloantonio.play.database.AppDatabase;
import com.cappielloantonio.play.database.dao.SongArtistCrossDao;
import com.cappielloantonio.play.database.dao.SongGenreCrossDao;
import com.cappielloantonio.play.model.SongArtistCross;
import com.cappielloantonio.play.model.SongGenreCross;
import java.util.List;
public class SongGenreRepository {
private static final String TAG = "AlbumArtistRepository";
private SongGenreCrossDao songGenreCrossDao;
public SongGenreRepository(Application application) {
AppDatabase database = AppDatabase.getInstance(application);
songGenreCrossDao = database.songGenreCrossDao();
}
public void insertAll(List<SongGenreCross> crosses) {
try {
final Thread delete = new Thread(new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songGenreCrossDao, crosses));
delete.start();
delete.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void deleteAll() {
DeleteAllSongGenreCrossThreadSafe delete = new DeleteAllSongGenreCrossThreadSafe(songGenreCrossDao);
Thread thread = new Thread(delete);
thread.start();
}
private static class InsertAllThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
private List<SongGenreCross> crosses;
public InsertAllThreadSafe(SongGenreCrossDao songGenreCrossDao, List<SongGenreCross> crosses) {
this.songGenreCrossDao = songGenreCrossDao;
this.crosses = crosses;
}
@Override
public void run() {
songGenreCrossDao.insertAll(crosses);
}
}
private static class DeleteAllSongGenreCrossThreadSafe implements Runnable {
private SongGenreCrossDao songGenreCrossDao;
public DeleteAllSongGenreCrossThreadSafe(SongGenreCrossDao songGenreCrossDao) {
this.songGenreCrossDao = songGenreCrossDao;
}
@Override
public void run() {
songGenreCrossDao.deleteAll();
}
}
}

View file

@ -215,6 +215,18 @@ public class SongRepository {
}
public void insertAll(ArrayList<Song> songs) {
try {
final Thread deleteAll = new Thread(new DeleteAllSongThreadSafe(songDao));
final Thread insertAll = new Thread(new InsertAllThreadSafe(songDao, songGenreCrossDao, songs));
deleteAll.start();
deleteAll.join();
insertAll.start();
insertAll.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
InsertAllThreadSafe insertAll = new InsertAllThreadSafe(songDao, songGenreCrossDao, songs);
Thread thread = new Thread(insertAll);
thread.start();
@ -405,8 +417,6 @@ public class SongRepository {
@Override
public void run() {
songDao.deleteAll();
songGenreCrossDao.deleteAll();
songDao.insertAll(songs);
}
}