【发布时间】:2015-11-17 10:40:28
【问题描述】:
我正在尝试概括我的班级结构。
我将更具体地展示我的真实结构。
我正在编写支持离线模式的应用程序,所以我决定使用 Robospice 和 GreenDao ORM 来实现我的 ETag 缓存机制。
我只需要缓存 GET 请求。
首先我的请求应该扩展基本请求(不是我的),在我的情况下是RetrofitSpiceRequest<T, V>
T is type of return data
V is service type, in my case I am using Retrofit.
问题是默认情况下返回类型不是List of T 类型,我需要创建扩展 T 对象数组并将其用作返回类型的子类。
像这样的
public class City {
....
....
....
public static class List extends ArrayList<City> {
.....
.....
}
}
并使用 City.List 作为返回类型。
但我的 DAO 声明如下
public class CityDao extends AbstractDao<City, Long> {
}
在每个请求 (GET) 中,如果数据与服务器数据不同,我需要将特定的 DAO 作为成员,以便缓存数据。或者如果没有连接,则从本地数据库加载数据。
这里的问题是由 T 类型生成的请求主要是列表,在我的例子中是 City.List,一些对象,但我的 dao 是由例如 E 类型生成的,在我的例子中是 City。
我想创建这样的方法
public AbastractDao<T,Long> getRequestDao() {
}
但就我的Request返回City.List而言,我不知道如何生成这个类,我觉得可以,但现在没有想法。
如果是非通用 dao 方法,我必须像这样复制代码
@Override
public void insertReceivedData(City.List received) {
mCityDao.insertOrReplaceInTx(received);
}
@Override
public City.List getCachedData() {
if (mFilterMap != null && mFilterMap.size() > 0) {
return (City.List) mCityDao.loadAll();
} else {
WhereCondition[] whereConditions = QueryUtils.convertPropertyMapToConditionalArray(mFilterMap);
return (City.List) mCityDao.queryBuilder().where(whereConditions[0], Arrays.copyOfRange(whereConditions, 1, whereConditions.length)).list();
}
}
在每个请求中
请分享你的想法。
谢谢。
【问题讨论】:
-
我理解对了吗,您希望您的服务自动注入并调用正确的 Dao?
-
是的,但问题是 Dao 是扩展 AbstractDao
并且主要用作 AbstractDao 其中 T 是映射到我的案例城市中的表的类型,但是我需要获取城市列表,就请求返回类 TI 的对象而言,必须创建自定义类 MyCityList extends ArrayList ,这就是问题所在。
标签: java android generics caching robospice