Sugar ORM does not save data in database

I am currently using Sugar ORM and Android Async Http Client for my Android application.

I read the Sugar ORM documentation and did exactly what it says. My HttpClient uses a singleton pattern and provides methods for calling some APIs.

Now comes the bad part about it. I cannot permanently save data in my database created by Sugar ORM. Here is the method that calls the API:

public void getAvailableMarkets(final Context context, final MarketAdapter adapter) { String url = BASE_URL.concat("/markets.json"); client.addHeader("Content-Type", "application/json"); client.addHeader("Accept", "application/json"); client.get(context, url, null, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { Log.i(TAG, "Fetched available markets from server: " + response.toString()); Result<Markets> productResult = new Result<Markets>(); productResult.setResults(new Gson().<ArrayList<Markets>>fromJson(response.toString(), new TypeToken<ArrayList<Markets>>() { }.getType())); ArrayList<Markets> marketsArrayList = productResult.getResults(); // This lines tells me that there are no entries in the database List<Markets> marketsInDb = Markets.listAll(Markets.class); if(marketsInDb.size() < marketsArrayList.size() || marketsInDb.size() > marketsArrayList.size()) { Markets.deleteAll(Markets.class); for(Markets m : marketsArrayList) { Markets market = new Markets(m.getId(), m.getName(), m.getChainId(), m.getLat(), m.getLng(), m.getBusinessHourId(), m.getCountry(), m.getZip(), m.getCity(), m.getStreet(), m.getPhoto(), m.getIcon(), m.getUrl()); market.save(); adapter.add(market); } adapter.notifyDataSetChanged(); } List<Markets> market = Markets.listAll(Markets.class); // This lines proves that Sugar ORM is not saving the entries Log.i(TAG, "The market database list has the size of:" + market.size()); } }); } 

This is what Logcat prints:

 D/Sugar: Fetching properties I/Sugar: Markets saved : 3 I/Sugar: Markets saved : 5 I/RestClient: The market database list has the size of:0 

I also looked at the ORAR Sugar tag here on stackoverflow, but no answers or questions could give me any hints on how to solve this problem. I am new to the Android ecosystem and will love any help from you guys to solve this problem. thanks in advance

+5
source share
1 answer

I just solve the same problem as yours. It was a pain in the neck, but after a few hours I found out what caused this problem.

Using Sugar ORM, you should not set the id property, which belongs to the SugarRecord class, otherwise ORM will try to update the objects, rather than insert them. Since I need to have a field with my object id, I used the json annotation to assign it to another field. The final step was to configure GSON to exclude fields without an Expose annotation.

So my class is as follows:

 public class MyClass { @Expose @SerializedName("id") private long myId; @Expose private String field1; @Expose private String field2; @Expose private byte[] field3; @Expose private double field4; public MyClass() { } // parametrized constructor and more logic } 

Hurrah!

+7
source

Source: https://habr.com/ru/post/1203823/


All Articles