I'm using Sugar ORM to save and retrieve data persistently. Below is the code that I'm using to store and retrieve data.
Slider.deleteAll(Slider.class);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Constants.URL, jsonRepos.getSliderJobj(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(Constants.MAINACTIVITY,response.toString());
try {
JSONArray sliderArray = response.getJSONArray("slider");
for (int i=0; i<sliderArray.length(); i++){
JSONObject sliderArrayJSONObject = sliderArray.getJSONObject(i);
String id = sliderArrayJSONObject.getString("id");
String albumId = sliderArrayJSONObject.getString("album_id");
String title = sliderArrayJSONObject.getString("title");
String imgUrl = sliderArrayJSONObject.getString("img_url");
String file = sliderArrayJSONObject.getString("file");
Log.d(Constants.MAINACTIVITY,file);
Log.d("Note", "saving");
Slider slider = new Slider(id, albumId, title , file, imgUrl);
slider.save();
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
appController.getErrorListener()
);
appController.getRequestQueue().add(jsonObjectRequest);
The problem is that I'm trying to truncate all records in Slider before adding data to the Slider. But it is not getting truncated, so the values are getting duplicated. How can I prevent this data duplication by truncating the table before adding any new value?