I'm using Room as the database for the app. I`m fetching data from the server using Retrofit. the scenario is i have a class called Photo and annotated with Entity to be used by room, and im using this class to map the response of the API using retroft. i need to create two tables using the same class for example: latest_photos table and popular_photos table. How can i achieve this.
I have an idea of using a single table through inserting a new column that indicates weather the photo is popular or latest but i dont know how to implement it efficientlly.
public void insertPhotos(final List photos) { ioExecutor.execute(new Runnable() { @Override public void run() { photoDao.bulkInsert(updateList(photos)); } }); }
here im inserting list of photos (consists of 20 photo object). how can i add a new field to every photo object. i`ve tried to use for loop but it takes noticeable time as i dont receive only 20 items but im paginating through the web server.
Ive solved the problem,now im dealing with only one table. i`ve inserted a new column that indicates weather the photo is popular or latest but the problem is the server is not returning any useful data to fill this column. so the only solution is to modify the json response before mapping to to my entity. so making retrofit call returning type to be ResponseBody to return json string itself and not to map it, so i can add a property to json response then i map it with the newly add property.
private void processData(Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONArray hits = jsonObject.getJSONArray("hits");
List<Photo> photos = new ArrayList<>();
for (int i = 0; i < hits.length(); i++) {
JSONObject photoJSONObject = hits.getJSONObject(i);
photoJSONObject.put("order", order); // add new property
String json = photoJSONObject.toString();
Gson gson = new Gson();
Photo photo = gson.fromJson(json, Photo.class);
photos.add(photo);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.