从Sugar db保存和检索数据而不重复

时间:2016-08-13 11:55:25

标签: android sugarorm

我正在使用Sugar ORM来持久保存和检索数据。下面是我用来存储和检索数据的代码。

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);

问题是我在向Slider添加数据之前试图截断Slider中的所有记录。但它没有被截断,因此价值变得越来越重要。如何在添加任何新值之前截断表来防止此数据重复?

0 个答案:

没有答案