LiveData onChanged null results - android

EventDao
#Dao
public interface EventDao {
#Query("SELECT * FROM events WHERE date_start = :date")
LiveData<List<EventPatientLocation>> test(Date date);
}
EventRepository
public class EventRepository {
private EventDao eventDao;
public EventRepository(Application application) {
MyDatabase db = MyDatabase.getDatabase(application);
eventDao = db.eventDao();
}
public LiveData<List<EventPatientLocation>> test(Date date) {
return eventDao.test(date);
}
EventViewModel
public class EventViewModel extends AndroidViewModel {
private EventRepository repository;
public EventViewModel(#NonNull Application application) {
super(application);
repository = new EventRepository(application);
}
public LiveData<List<EventPatientLocation>> test(Date date) {
return repository.test(date);
}
}
Fragment (onActivityCreated)
eventViewModel = ViewModelProviders.of(activity).get(EventViewModel.class);
LiveData<List<EventPatientLocation>> testEvent = eventViewModel.test(new Date());
testEvent.observe(activity, events -> {
// events is always null!
});
Despite the onChanged callback on fragment is called multiple times, the events list is always null...I know for sure that in database there are candidate rows for the query, so where am I doing wrong? Help please...

Related

I'm using a Room database in Android Studio, and I can't seem to update correctly a field of an item in the database

I've been doing Android Studio for a month only and I've got to say I'm kind of confused with Room database, I'm sorry if the question sounds confused.
I'm using a Room database as stated, here are my Database-related classes:
#Entity(tableName="board")
public class BoardItem {
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name="board_id")
private int boardId;
#ColumnInfo(name="board_name")
private String boardName;
#ColumnInfo(name="board_description")
private String boardDescription;
#ColumnInfo(name="board_image_list")
#TypeConverters(ImageListTypeConverter.class)
private List<ImageItem> boardImageList;
public BoardItem(String boardName, String boardDescription){
this.boardName = boardName;
this.boardDescription = boardDescription;
this.boardImageList = new ArrayList<>();
}
public int getBoardId() { return boardId; }
public String getBoardName() {
return boardName;
}
public String getBoardDescription() {
return boardDescription;
}
public String getPhotosCount() {
return String.valueOf(this.boardImageList.size());
}
public void setBoardId(int boardId) {
this.boardId = boardId;
}
public List<ImageItem> getBoardImageList() {
return boardImageList;
}
public void setBoardImageList(List<ImageItem> list) { this.boardImageList = list; }
#Dao
public interface BoardItemDAO {
#Insert(onConflict = OnConflictStrategy.IGNORE)
void addBoardItem(BoardItem boardItem);
#Transaction
#Query("SELECT * from board ORDER BY board_id DESC")
LiveData<List<BoardItem>> getBoardItems();
#Transaction
#Query("SELECT * from board ORDER BY board_id DESC")
List<BoardItem> getBoardItemsNow();
}
#Database(entities = {BoardItem.class}, version = 1)
public abstract class BoardItemDatabase extends RoomDatabase {
public abstract BoardItemDAO boardItemDAO();
//Singleton
private static volatile BoardItemDatabase INSTANCE;
private static final int NUMBER_OF_THREADS = 4;
static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
static BoardItemDatabase getDatabase(final Context context){
if (INSTANCE == null) {
synchronized (BoardItemDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), BoardItemDatabase.class, "board_database")
.allowMainThreadQueries()
.build();
}
}
}
return INSTANCE;
}
}
//Repository
public class BoardItemRepository {
private BoardItemDAO boardItemDAO;
private LiveData<List<BoardItem>> boardItemList;
private List<BoardItem> boardItemListNow;
public BoardItemRepository (Application application) {
BoardItemDatabase db = BoardItemDatabase.getDatabase(application);
boardItemDAO = db.boardItemDAO();
boardItemList = boardItemDAO.getBoardItems();
boardItemListNow = boardItemDAO.getBoardItemsNow();
}
//Room executes all queries on a separate thread
//Observed LiveData will notify the observer when data has changed
public LiveData<List<BoardItem>> getBoardItemList() { return boardItemList; }
//this method is called on a non-UI thread or the app will throw an exception. Room ensures
//that there are no long running operations on the main thread, blocking the UI.
public void addBoardItem(final BoardItem boardItem) {
BoardItemDatabase.databaseWriteExecutor.execute(() -> boardItemDAO.addBoardItem(boardItem));
}
public List<BoardItem> getBoardItemListNow() {return boardItemListNow; }
}
Note that BoardItem and ImageItem are classes I made myself: a board is supposed to contain multiple ImageItems.
My boardItem has different fields, one of which is a list of ImageItems.
Now, in a specific fragment I try to update this list of ImageItems in a board that already exists in my database, which is the board with id = 0 (the very first board in the db). I try to retrieve the list from the Database and replace it with a new one.
I have used LiveData in certain cases to update the view of my app when the item change, but I have non LiveData methods for this specific piece of my code I need to change my database as soon as I click the button that contains this code:
List<BoardItem> boardItems = boardListViewModel.getBoardItemsNow();
newList = boardItems.get(0).getBoardImageList();
newList.add(newItem);
boardItems.get(0).setBoardImageList(newList);
When I click the button, the code is executed with no errors, but the database isn't updated; it contains the list as it was before, without the new item.
Thanks in advance, again I'm sorry if this sounds confusing!
EDIT:
here's my ViewModel:
public class BoardListViewModel extends AndroidViewModel {
private final MutableLiveData<BoardItem> boardSelected = new MutableLiveData<>();
private LiveData<List<BoardItem>> boardItems;
private List<BoardItem> boardItemsNow;
public BoardListViewModel(#NonNull Application application) {
super(application);
BoardItemRepository repo = new BoardItemRepository(application);
boardItems = repo.getBoardItemList();
boardItemsNow = repo.getBoardItemListNow();
}
public void select(BoardItem boardItem) {
boardSelected.setValue(boardItem);
}
public LiveData<BoardItem> getSelected() {
return boardSelected;
}
public LiveData<List<BoardItem>> getBoardItems() {
return boardItems;
}
public BoardItem getBoardItem(int position) {
return boardItems.getValue() == null ? null : boardItems.getValue().get(position);
}
public List<BoardItem> getBoardItemsNow() { return boardItemsNow; }
}
I believe that your issue is that you are not updating but attempting to add (insert) a new item (row) via :-
#Insert(onConflict = OnConflictStrategy.IGNORE)
void addBoardItem(BoardItem boardItem);
As the boardid already exists and that it is the primary key, which is implicitly unique, a conflict occurs, and this conflict is ignored, thus the new row is not added and the database remains unchanged.
What you should be doing is updating the existing row, so you want an #Update dao.
So add and then use the following dao :-
#Update(onConflict = OnConflictStrategy.IGNORE)
int updateBoardItem(BoardItem boardItem);
the int returned is the number of rows that have been update (you would expect 1, 0 if a conflict resulted in the update being ignored).

Why database isn't showed in Stetho

I have a database in my app. Here's the way how I create it:
App class:
public class TraktTvApp extends Application {
private static Context sAppContext;
public static TraktTvApp instance;
private MovieDatabase database;
#Override
public void onCreate() {
super.onCreate();
sAppContext = getApplicationContext();
instance = this;
database = Room.databaseBuilder(this, MovieDatabase.class, "MovieDatabase").build();
}
#NonNull
public static Context getAppContext() {
return sAppContext;
}
public static TraktTvApp getInstance() {
return instance;
}
public MovieDatabase getDatabase() {
return database;
}
}
DAO class
#Dao
public interface MovieDao {
#Query("SELECT * from MovieEntity")
List<MovieEntity> getFavorites();
#Insert(onConflict = OnConflictStrategy.REPLACE)
Completable insertMovie(final MovieEntity movie);
#Delete
void deleteMovie(MovieEntity movie);
}
Database class
#Database(entities = {MovieEntity.class}, version = 1)
public abstract class MovieDatabase extends RoomDatabase {
public abstract MovieDao movieDao();
}
And here's the way how I call insert method:
mCompositeDisposable.add(Observable.fromCallable(()->movieDao.insertMovie(movieEntity))
.doOnSubscribe(disposable -> mView.showLoadingIndicator(true))
.doOnComplete(() -> {
mView.showEmptyState(false);
mView.onMoviesAdded();
})
.doOnError(throwable -> mView.showEmptyState(true))
.doOnTerminate(() -> mView.showLoadingIndicator(false))
.observeOn(Schedulers.io())
.subscribe());
But when I want to check data in my database in Stetho, there's nothing here:
So, what's the matter and how can I solve this problem? It seems to me that it can be problem in creating database, but I used the same way as usual and usually it works ok
call setupDebugTools() in application's onCreate() like
{
super.onCreate()
setupDebugTools()
}
And
private void setupDebugTools() {
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this)
}
}
for more information

LiveData does not show data Instantly on opening app

The Issue
I implemented the Android Architecture library and with this I am restoring data from Room Database, with MVVM (Model View View-Model) and whenever I use the observer to view LiveDada every time I start the app there is a noticeable delay when the items from the recycler view loads.
Image illustrates on what I mean when the app stats and the items show delay when they load.
What I want it to do
What I want to achieve with LiveData is this and the way I was able to achieve this is in my Dao I used a Query to get all of the data and pass it as a List rather than using LiveData and then in the Repository convert it to MutableLiveData and then pass it to database and from there observe it as LiveData in my fragment but using this approach actually doesn't update on delete or on insert unless if i restart the app.
Is there any way to fix this issue?
I would much like to use LiveData.
This is my DevicesDao interface:
#Dao
public interface DevicesDao {
#Insert
void insert(Devices... devices);
#Query("SELECT * FROM devices")
LiveData<List<Devices>> getDevices();
/*
#Query("SELECT * FROM devices")
List<Devices> getDevices();
*/
#Delete
void delete(Devices... device);
#Update
void update(Devices... device);
}
Database:
#Database(entities = {Devices.class}, version = 1)
public abstract class DevicesDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "devices_registered";
private static DevicesDatabase instance;
public abstract DevicesDao devicesDao();
public static DevicesDatabase getInstance(final Context context) {
if (instance == null) {
synchronized (DevicesDatabase.class) {
if (instance == null) {
instance = Room.databaseBuilder(
context.getApplicationContext(),
DevicesDatabase.class,
DATABASE_NAME)
.fallbackToDestructiveMigration()
.build();
}
}
}
return instance;
}
}
Repository:
public class DevicesRepository {
private final DevicesDao devicesDao;
public DevicesRepository(Application application) {
DevicesDatabase db = DevicesDatabase.getInstance(application);
devicesDao = db.devicesDao();
}
public void addDevices(Devices devices) {
new InsertDeviceAsync(devicesDao).execute(devices);
}
public void updateDevice(Devices devices) {
new UpdateDeviceAsync(devicesDao).execute(devices);
}
public void deleteDevice(Devices devices) {
new DeleteDeviceAsync(devicesDao).execute(devices);
}
//Gets all data from SQLite
public LiveData<List<Devices>> getAllDevices() {
return devicesDao.getDevices();
}
/*
public LiveData<List<Devices>> getAllDevices() {
MutableLiveData<List<Devices>> devices = new MutableLiveData<>();
try {
devices.setValue(new GetDeviceAsync(devicesDao).execute().get());
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return devices;
}
*/
}
View Model File:
public class HomeFragmentViewModel extends AndroidViewModel {
private final DevicesRepository devicesRepository;
private LiveData<List<Devices>> devices;
public HomeFragmentViewModel(#NotNull Application application) {
super(application);
devicesRepository = new DevicesRepository(application);
devices = devicesRepository.getAllDevices();
}
public LiveData<List<Devices>> getAllDevices() {
return devices;
}
public void addNewDevice(Devices devices) {
devicesRepository.addDevices(devices);
}
public void deleteDevice(Devices devices) {
devicesRepository.deleteDevice(devices);
}
public void editDevice(Devices devices) {
devicesRepository.updateDevice(devices);
}
}
And lasty, the observer in my fragment:
///////Other code
//Implements ViewModel to HomeFragment
homeFragmentViewModel = ViewModelProviders.of(this).get(HomeFragmentViewModel.class);
homeFragmentViewModel.getAllDevices().observe(getViewLifecycleOwner(), devicesList -> {
//Validation tool
validationUtil = new ValidationUtil(devicesList);
//Adds to adapter
adapter.submitList(devicesList);
/////// Other code
});
Thank you for your time!!!
New update: When I reset the app data, it loads fine but as soon as I decide to update the code, the issue comes back regardless of that edit I make in Android Studio.

Why is Android Room not assinging my auto generated id as soon as I create my object?

I am creating an app that requires a team of players. I am using the Team ID as the team primary key and the foreign key for each player. In one fragment I create a new team. When the team is created and added to my room database it initially has the ID of 0 or not set even though I have auto generate set to true. I then navigate to the team roster view which has the ability to add new players to the team. When I create a new player and use the new teams ID in the team view model the team ID is still 0 or not set so the app crashes and there is a foreign key restraint failure. After the crash if I reopen the app or if I avoid the crash by going back to the team list and selecting the team that was just created with an initial id of 0, when I create a player this time the team will have a valid ID. Why does room not immediately assign a unique ID when the object is created and it waits for navigation away and back to the fragment or a restart of the app? Relevant code below, feels like I might be giving too code much but I am following jetpack best practices that I found from android documentation, and I do not know where the problem is stemming from. https://developer.android.com/jetpack/docs/guide.
Database
#Database (entities = {Team.class,
Player.class},
version = 6)
public abstract class AppDatabase
extends RoomDatabase
{
private static final String DATABASE_NAME = "Ultimate_Stats_Database";
private static volatile AppDatabase instance;
public abstract TeamDAO teamDao ();
public abstract PlayerDAO playerDAO ();
static synchronized AppDatabase getInstance (Context context)
{
if (instance == null)
{
// Create the instance
instance = create(context);
}
// Return the instance
return instance;
}
private static AppDatabase create (final Context context)
{
// Create a new room database
return Room.databaseBuilder(
context,
AppDatabase.class,
DATABASE_NAME)
.fallbackToDestructiveMigration() // TODO Add migrations, poor practice to ignore
.build();
}
}
Team entity
#Entity (tableName = "teams")
public class Team
implements Parcelable
{
#PrimaryKey (autoGenerate = true)
private long id;
private String name;
public Team ()
{
this.name = "";
}
public Team (String name)
{
this.name = name;
}
...
Team DAO
#Dao
public abstract class TeamDAO
{
#Insert (onConflict = OnConflictStrategy.REPLACE)
public abstract long insert (Team team);
#Delete
public abstract int deleteTeam (Team team);
#Query ("SELECT * FROM teams")
public abstract LiveData<List<Team>> getAllTeams ();
}
Team Repository (Inserting Only)
private TeamDAO teamDao;
private LiveData<List<Team>> teams;
public TeamRepository (Application application)
{
AppDatabase db = AppDatabase.getInstance(application);
teamDao = db.teamDao();
teams = teamDao.getAllTeams();
}
private static class insertAsyncTask
extends AsyncTask<Team, Void, Void>
{
private TeamDAO asyncTeamTaskDao;
insertAsyncTask (TeamDAO teamDao)
{
asyncTeamTaskDao = teamDao;
}
#Override
protected Void doInBackground (final Team... params)
{
// Trace entry
Trace t = new Trace();
// Insert the team into the database
asyncTeamTaskDao.insert(params[0]);
// Trace exit
t.end();
return null;
}
}
Team View Model
public class TeamViewModel
extends AndroidViewModel
{
private TeamRepository teamRepository;
private LiveData<List<Team>> teams;
private MutableLiveData<Team> selectedTeam;
public TeamViewModel (Application application)
{
super(application);
teamRepository = new TeamRepository(application);
teams = teamRepository.getAllTeams();
selectedTeam = new MutableLiveData<Team>();
}
public LiveData<Team> getSelectedTeam()
{
return selectedTeam;
}
public void selectTeam(Team team)
{
selectedTeam.setValue(team);
}
public LiveData<List<Team>> getTeams ()
{
return teams;
}
public void insert (Team team)
{
teamRepository.insert(team);
}
...
Player entity
#Entity(tableName = "players",
foreignKeys = #ForeignKey(entity = Team.class,
parentColumns = "id",
childColumns = "teamId"),
indices = {#Index(value = ("teamId"))})
public class Player
implements Parcelable
{
#PrimaryKey (autoGenerate = true)
private long id;
private String name;
private int line;
private int position;
private long teamId;
public Player ()
{
this.name = "";
this.line = 0;
this.position = 0;
this.teamId = 0;
}
public Player(String name,
int line,
int position,
long teamId)
{
this.name = name;
this.line = line;
this.position = position;
this.teamId = teamId;
}
....
Player DAO
#Dao
public abstract class PlayerDAO
{
#Insert (onConflict = OnConflictStrategy.REPLACE)
public abstract void insert (Player player);
#Delete
public abstract int deletePlayer (Player player);
#Query ("SELECT * FROM players WHERE teamId = :teamId")
public abstract LiveData<List<Player>> getPlayersOnTeam (long teamId);
#Query ("SELECT * FROM players")
public abstract LiveData<List<Player>> getAllPlayers();
#Query ("SELECT * FROM players WHERE id = :id")
public abstract LiveData<Player> getPlayerById (long id);
}
Player Repository (Inserting Only)
private PlayerDAO playerDAO;
private LiveData<List<Player>> players;
public PlayerRepository(Application application)
{
AppDatabase db = AppDatabase.getInstance(application);
playerDAO = db.playerDAO();
players = playerDAO.getAllPlayers();
}
public void insert (Player player)
{
new PlayerRepository.insertAsyncTask(playerDAO).execute(player);
}
private static class insertAsyncTask
extends AsyncTask<Player, Void, Void>
{
private PlayerDAO asyncTaskDao;
insertAsyncTask (PlayerDAO dao)
{
asyncTaskDao = dao;
}
#Override
protected Void doInBackground (final Player... params)
{
// Get the player being inserted by its id
LiveData<Player> player = asyncTaskDao.getPlayerById(((Player) params[0]).getId());
if (player != null)
{
// Delete the old record of the player
asyncTaskDao.deletePlayer(params[0]);
}
// Insert the player into the database
asyncTaskDao.insert(params[0]);
return null;
}
}
...
Player View Model
public class PlayerViewModel
extends AndroidViewModel
{
private PlayerRepository playerRepository;
private LiveData<List<Player>> players;
private MutableLiveData<Player> selectedPlayer;
public PlayerViewModel(Application application)
{
super(application);
playerRepository = new PlayerRepository(application);
players = playerRepository.getAllPlayers();
selectedPlayer = new MutableLiveData<Player>();
}
public LiveData<Player> getSelectedPlayer()
{
return selectedPlayer;
}
public void selectPlayer(Player player)
{
selectedPlayer.setValue(player);
}
public LiveData<List<Player>> getPlayers ()
{
return players;
}
public void insert (Player player)
{
playerRepository.insert(player);
}
...
Where I create the team (in the TeamListFragment and when a dialog fragment is completed)
public void onDialogPositiveClick (String teamName)
{
// Trace entry
Trace t = new Trace();
// Create a new team object
Team newTeam = new Team();
// Name the new team
newTeam.setName(teamName);
// Insert the team into the database and set it as the selected team
teamViewModel.insert(newTeam);
teamViewModel.selectTeam(newTeam);
// Trace exit
t.end();
// Go to the player list view
routeToPlayerList();
}
In the playerListFragment when it is created
/*------------------------------------------------------------------------------------------------------------------------------------------*
* If the view model has a selected team *
*------------------------------------------------------------------------------------------------------------------------------------------*/
if (sharedTeamViewModel.getSelectedTeam().getValue() != null)
{
// Set the team to the team selected
team = sharedTeamViewModel.getSelectedTeam().getValue();
// Set the team name fields default text
teamNameField.setText(team.getName());
}
In the playerFragment when the save button is clicked
#Override
public void onClick (View v)
{
// Trace entry
Trace t = new Trace();
// Update the player object with the info given by the user
boolean success = getUserInput();
/*------------------------------------------------------------------------------------------------------------------------------*
* If the input was valid *
*------------------------------------------------------------------------------------------------------------------------------*/
if (success)
{
// Set the player id to the team that is selected
player.setTeamId(sharedTeamViewModel.getSelectedTeam()
.getValue()
.getId());
// Input the the player into the player view model
sharedPlayerViewModel.insert(player);
// Remove this fragment from the stack
getActivity().onBackPressed();
}
// Trace exit
t.end();
}
If there is any other code that is needed let me know
This is an expected behavior. Room will not directly update the id field in the newTeam.
It does not make sense for Room to change the input object, not to mention that Room does not assume that the entity fields are mutable. You can make all your Entity fields immutable and I believe it is a good practice to make your entity classes immutable whenever possible.
If you want to retrieve the id of the inserted row, check out this SO link: Android Room - Get the id of new inserted row with auto-generate

Combine Room and Retroffit with Dagger2

What the proper way to create DAO with Room and Retrofit?
I have database module like this:
#Module
public class ApplicationDatabaseModule {
private final String mDatabaseName;
ApplicationDatabase mApplicationDatabase;
public ApplicationDatabaseModule(#ApplicationContext Context context, Class<? extends ApplicationDatabase> roomDataBaseClass, String databaseName) {
mDatabaseName = databaseName;
mApplicationDatabase = Room.databaseBuilder(context, roomDataBaseClass, mDatabaseName).build();
}
#Singleton
#Provides
ApplicationDatabase provideApplicationDatabase() {
return mApplicationDatabase;
}
#Singleton
#Provides
CitiesDao provideCitiesDao() {
return mApplicationDatabase.getCitiesDao();
}
}
POJO class like this:
#Entity
public class City {
#PrimaryKey
#ColumnInfo(name = "id")
private int cityId;
#ColumnInfo(name = "name")
private String cityName;
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
#Override
public String toString() {
return "City [cityId = " + cityId + ", cityName = " + cityName + "]";
}
}
DAO interface like this:
#Dao
public interface CitiesDao {
#Insert
void insertCities(City... cities);
#Query("SELECT * FROM City")
City[] queryCities();
}
And API for Retrofit:
public interface CitiesApi {
#GET("/api/cities")
Call<City[]> requestCities();
}
As I know DAO is responsible for accessing data, including data passed through REST-client. But these two parts are represented by interfaces and built into separate classes. What is the proper way to implement DAO?
DAO is responsible for accessing data
yes
, including data passed through REST-client.
God no
What is the proper way to implement DAO?
Room already generates a proper way of implementation for your DAO based on your interface + annotations, I think it's called CitiesDao_Impl.
What the proper way to create DAO with Room and Retrofit?
Room doesn't know about Retrofit and shouldn't need to know about Retrofit. It only cares about local data persistence.
Meaning your DAO needs to look like this:
#Dao
public interface CitiesDao {
#Insert
#Transaction
void insertCities(City... cities);
#Query("SELECT * FROM City")
LiveData<List<City>> queryCities();
}
So what you actually need is a Worker that will fetch new data in background when either cache is invalid (force fetch new data) or when your sync task should run (for example when device is charging and you are on WIFI and you're at 2 AM to 7 AM -- for this you'd need WorkManager).
Immediately fetching new data though is fairly easy, all you need is either an AsyncTask in a singleton context that returns null from doInBackground, or your own Executor that you post your background task to.
public class FetchCityTask extends AsyncTask<Void, Void, Void> {
...
#Override
protected Void doInBackground(Void... params) {
List<City> cities = citiesApi.requestCities().execute().body(); // TODO error handling
citiesDao.insertCities(cities);
return null;
}
}
And then
new FetchCityTask(...).execute();
Now when this task runs, your UI will be updated with latest data by observing the LiveData that you store in a ViewModel.
public class CitiesViewModel
extends ViewModel {
private final CitiesDao citiesDao;
private LiveData<List<City>> liveResults;
public CitiesViewModel(...) {
...
liveResults = citiesDao.queryCities();
}
public LiveData<List<City>> getCities() {
return liveResults;
}
}
And
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recycler_view);
CitiesViewModel viewModel = ViewModelProviders.of(this).get(CitiesViewModel.class, ...);
...
viewModel.getTasks().observe(getViewLifecycle(), list -> {
//noinspection Convert2MethodRef
listAdapter.submitList(list);
});
}
You want to create a repository class to handle your data. Then you simply interact with your repository. Some pseudocode for you:
class Repository {
private CitiesDao localSource;
private CitiesApi remoteSource;
public Repository() {
//initialize objects here
}
City[] getCities() {
if (networkIsAvailable) {
City[] cities = remoteSource.requestCities();
saveCitiesToDatabase(cities);
return cities;
} else {
return localSource.queryCities();
}
}
private void saveCitiesToDatabase(City[] cities) {
//TODO save cities to databse
}
}

Categories

Resources