Reputation: 534
ViewModel, Repository and Dao class are mentioned below.
Instantiate Viewmodel in Fragment.
playlistVideoViewModel = ViewModelProviders.of(this).get(PlaylistVideoViewModel.class);
playlistVideoViewModel.getAllPlaylistVideos(String.valueOf(currentBucketId)).observe(this, new PlaylistSongObserver());
View Model class:
public class PlaylistVideoViewModel extends AndroidViewModel {
private MutableLiveData<List<PlaylistSong>> allPlaylistSongs;
private PlaylistVideoRepo playlistVideoRepo;
public PlaylistVideoViewModel(@NonNull Application application) {
super(application);
playlistVideoRepo = new PlaylistVideoRepo(application);
allPlaylistSongs = new MutableLiveData<>();
}
public LiveData<List<PlaylistSong>> getAllPlaylistVideos(String playlistId) {
new GetPlaylistSongs().execute(playlistId);
return allPlaylistSongs;
}
public void delete(String path) {
playlistVideoRepo.delete(path);
}
@SuppressLint("StaticFieldLeak")
class GetPlaylistSongs extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
allPlaylistSongs.postValue(playlistVideoRepo.getAllPlaylistSongs(strings[0]));
return null;
}
}}
Repository:
class PlaylistVideoRepo {
private final PlaylistSongDao playlistSongDao;
PlaylistVideoRepo(Application application) {
AppDatabase appDatabase = AppDatabase.getDatabase(application);
playlistSongDao = appDatabase.playlistSongDao();
}
public List<PlaylistSong> getAllPlaylistSongs(String playlistId) {
return playlistSongDao.getAllPlaylistSongs(playlistId);
}
public void delete(String path) {
new DeletePlaylistVideoAsyncTask(playlistSongDao).execute(path);
}}
Dao:
@Dao
public interface PlaylistSongDao {
@Query("SELECT * FROM " + PlaylistVideoConstants.TABLE_NAME +
" WHERE " + PlaylistVideoConstants.Columns.PLAYLIST_ID + " = :playlistId")
List<PlaylistSong> getAllPlaylistSongs(String playlistId);
@Query("DELETE FROM "+ PlaylistVideoConstants.TABLE_NAME + " WHERE " + PlaylistVideoConstants.Columns.PATH + " = :path")
void delete(String path);}
Now issue is whenever i am going to delete any data. I am not receiving any callback in my observer.
Please rectify if I am doing wrong anything.
Upvotes: 2
Views: 2653
Reputation: 154
I am assuming that you are observing allPlaylistSongs
. When you are deleting by path, you are never updating that MutableLiveData
. There are a couple of ways you could solve your problem.
MutableLiveData
whenever you are deleting a song. So you could either reload all songs from repo by executing GetPlaylistSongs
task again.MutableLiveData
.LiveData
instead of just value. That way any changes in the database will be propagated to your observers.Example:
@Dao
public interface PlaylistSongDao {
@Query("SELECT * FROM " + PlaylistVideoConstants.TABLE_NAME +
" WHERE " + PlaylistVideoConstants.Columns.PLAYLIST_ID + " = :playlistId")
LiveData<List<PlaylistSong>> getAllPlaylistSongs(String playlistId);
@Query("DELETE FROM "+ PlaylistVideoConstants.TABLE_NAME + " WHERE " + PlaylistVideoConstants.Columns.PATH + " = :path")
void delete(String path);}
By changing it to LiveData
you would now be able to remove the GetPlaylistSongs
task and you could directly pass down the LiveData
object to Repository and then down to Fragment (of course applying any transformations you may need).
I hope this helps.
Upvotes: 1