Im building an app following architecture guidelines.Implemented room db caching + network.Need to get latest page number from separate entity.
My model:
@Entity(tableName = "top_rated_movie_page")
public class Top_Rated_Movies_Page {
@PrimaryKey(autoGenerate = true)
private int db_id;
private Integer page;
private Integer total_results;
private Integer total_pages;
@Ignore
private List<Result> results;
...
Result
class contains data which i display in my paged list which observes changes from db.
Using PagedList.BoundaryCallback
i need to fetch new data from network and insert it into db.
But i need to get page number somehow.
My dao:
@Insert
void insertAll(Top_Rated_Movies_Page page,List<Result> top_rated_results);
@Query("SELECT * FROM Result")
DataSource.Factory<Integer, Result> getAllResults();
@Query("SELECT * FROM top_rated_movie_page WHERE page= (SELECT MAX(page) FROM top_rated_movie_page)")
LiveData<Top_Rated_Movies_Page> getMoviePage();
I was thinking to observe Top_Rated_Movies_Page
from db in my repository class with observeForever()
to get that page number.
Is that the best way to approach this?
observeForever
and not have a LifecycleOwner, you're supposed to useTransformations.switchMap
instead. – Cliffhanger