使用Room and Retrofit来创建DAO的正确方法是什么?
我有这样的数据库模块:
@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类:
@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界面如下:
@Dao
public interface CitiesDao {
@Insert
void insertCities(City... cities);
@Query("SELECT * FROM City")
City[] queryCities();
}
用于翻新的API:
public interface CitiesApi {
@GET("/api/cities")
Call<City[]> requestCities();
}
据我所知,DAO负责访问数据,包括通过REST客户端传递的数据。但是这两部分由接口表示,并内置到单独的类中。实施DAO的正确方法是什么?
答案 0 :(得分:1)
DAO负责访问数据
是
,包括通过REST客户端传递的数据。
上帝没有
实施DAO的正确方法是什么?
Room已经根据您的界面+注释为DAO生成了一种正确的实现方式,我认为它称为CitiesDao_Impl
。
使用Room and Retrofit来创建DAO的正确方法是什么?
房间不了解改造,也不需要了解改造。它只关心本地数据的持久性。
表示您的DAO需要如下所示:
@Dao
public interface CitiesDao {
@Insert
@Transaction
void insertCities(City... cities);
@Query("SELECT * FROM City")
LiveData<List<City>> queryCities();
}
因此,您实际上真正需要的是一个Worker,它将在高速缓存无效(强制获取新数据)或应运行同步任务(例如设备正在充电时)时在后台获取新数据。并且您使用的是WIFI,并且是从凌晨2点到凌晨7点-为此,您需要WorkManager
)。
立即获取新数据虽然相当容易,但您所需要的只是在单例上下文中从null
返回doInBackground
的AsyncTask,或者您将自己的后台任务发布到自己的执行器。 / p>
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;
}
}
然后
new FetchCityTask(...).execute();
现在,当执行此任务时,将通过观察存储在ViewModel中的LiveData来用最新数据更新UI。
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;
}
}
和
@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);
});
}
答案 1 :(得分:0)
您想创建一个存储库类来处理您的数据。然后,您只需与存储库进行交互。一些伪代码供您使用:
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
}
}