是否可以通過Robospice/ORMlite緩存嵌套(2-3層嵌套)對象(外部字段)? https://groups.google.com/forum/#!msg/robospice/VGLB3-vM3Ug/-piOac212HYJ - 在那裏你可以閱讀這是可能的,但不幸的是我無法實現它。使用ORMLite緩存Robospice中的嵌套對象
這裏是我的源代碼:
@DatabaseTable(tableName = "city")
@JsonIgnoreProperties(ignoreUnknown = true)
public class City {
@DatabaseField(id = true)
@JsonProperty("id")
private long id;
@DatabaseField
@JsonProperty("name")
private String name;
@ForeignCollectionField(eager = true, maxEagerLevel = 3)
@JsonProperty("clubs")
private Collection<Club> clubs;
...
@DatabaseTable(tableName = "club")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Club {
@DatabaseField(id = true)
@JsonProperty("user_id")
private long id;
@DatabaseField
@JsonProperty("name")
private String name;
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "city_id", maxForeignAutoRefreshLevel = 2)
private City city;
@DatabaseField(foreign = true)
@JsonProperty("address")
private VenueAddress address;
...
@DatabaseTable(tableName = "address")
@JsonIgnoreProperties(ignoreUnknown = true)
public class VenueAddress {
@DatabaseField(id = true)
@JsonProperty("uid")
private long id;
@DatabaseField
@JsonProperty("street")
private String street;
@DatabaseField
@JsonProperty("street_number")
private String streetNumber;
@DatabaseField
@JsonProperty("country")
private String country;
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "club_id", maxForeignAutoRefreshLevel = 2)
private Club club;
...
而且樣品SpiceService:
public class SampleSpiceService extends SpringAndroidSpiceService {
private static final int WEBSERVICES_TIMEOUT = 10000;
@Override
public CacheManager createCacheManager(Application application) {
CacheManager cacheManager = new CacheManager();
List<Class<?>> classCollection = new ArrayList<Class<?>>();
// add persisted classes to class collection
classCollection.add(VenueAddress.class);
classCollection.add(City.class);
classCollection.add(Club.class);
// init
RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper(application,
"sample_database.db", 5);
InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory(
application, databaseHelper, classCollection);
cacheManager.addPersister(inDatabaseObjectPersisterFactory);
return cacheManager;
}
@Override
public RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// set timeout for requests
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
httpRequestFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
restTemplate.setRequestFactory(httpRequestFactory);
MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
restTemplate.getMessageConverters().add(messageConverter);
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
return restTemplate;
}
}
當我從緩存中讀取市對象有俱樂部集合,但VenueAddress在每個俱樂部都有空字段(除了id)。 你有什麼建議嗎?
比你的答案。在我描述的例子中,可以嵌入對象。但我有更多,更深的嵌套類。 但最終我選擇了不同的方式來緩存RoboSpice中的請求(默認情況下,在JacksonSpringAndroidSpiceService中提供)。就我而言,性能沒有任何差異。 –