在Repository类中观察Forever是一个好习惯吗? db + network页面列表 [英] Is it a good practice to observeForever in Repository class? db+network paged list

本文介绍了在Repository类中观察Forever是一个好习惯吗? db + network页面列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照体系结构准则构建应用.实现了会议室数据库缓存+网络.需要从单独的实体获取最新的页码.

Im building an app following architecture guidelines.Implemented room db caching + network.Need to get latest page number from separate entity.

我的模特:

@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类包含我在分页列表中显示的数据,该数据可观察到数据库的变化.

Result class contains data which i display in my paged list which observes changes from db.

使用PagedList.BoundaryCallback我需要从网络获取新数据并将其插入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();

我当时正在考虑使用observeForever()从我的存储库类中的db观察Top_Rated_Movies_Page以获得该页码. 那是解决这个问题的最好方法吗?

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?

推荐答案

由于只有通过BoundaryCallback才能读取下一页密钥或更新后备数据库,因此您只需读/写下一页密钥直接.

Since the only time you'll read the next page key or update the backing DB is through BoundaryCallback, you can just read / write your next page key directly.

因此,在您的onItemAtEndLoad()实现中,您需要以下内容:

So in your onItemAtEndLoad() implementation you want something like:

String nextMoviePage = db.runInTransaction(() -> {
    movieDao.nextRemoteKey().key;
});

// Make sure not to run on main thread
MovieNetworkResponse response = networkApi.fetchNextMoviePage(remoteKey);

db.runInTransaction(() -> {
    movieDao.clearAll(); // Remove previous key
    movieDao.insertKey(RemoteKey(response.nextPageKey)); // Insert new key
    movieDao.insertAll(response.movies); // Update DataSource + invalidate()
});

您的DAO:

@Insert
void insertAll(List<Result> top_rated_results);

@Query("SELECT * FROM Result")
DataSource.Factory<Integer, Result> getAllResults();

@Query("SELECT * FROM result_key LIMIT 1")
String nextRemoteKey();

@Insert
void insertKey(RemoteKey remoteKey);

当您希望刷新数据时,请不要忘记清除所有项和remoteKey!

And don't forget to clear out both the items and remoteKey whenever you expect to refresh the data!

如果要跟踪要查询的不同键,只需将该列添加到RemoteKey实体即可.

In the case where you want to keep track of different keys for query, you can simply add that column to your RemoteKey entity.

仅供参考:Paging2已被Paging3取代(尽管刚刚启动了alpha01),这是类似的Paging3示例,它完全解决了您的用例:

FYI: Paging2 has been superseded by Paging3 (though just launched alpha01), here is the similar Paging3 sample which solves exactly your use-case: https://github.com/android/architecture-components-samples/blob/master/PagingWithNetworkSample/app/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/repository/inDb/PageKeyedRemoteMediator.kt

这篇关于在Repository类中观察Forever是一个好习惯吗? db + network页面列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆