question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sugar ORM is not saving data into the database

See original GitHub issue

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

I read through the documentation of Sugar ORM and did exactly what is written there. My HttpClient is using the singleton pattern and provides methods for calling some APIs.

Now comes the bad part about it. I am not able to save the data persistently into my database which is created by Sugar ORM. Here is the method, that is calling an 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());
        }
    });
}

his is what Logcat is printing:

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

Also I took a look at the Sugar ORM tag at stackoverflow, but no answers or questions could give me a hint on how to solve that problem. I am a newbie to the android ecosystem and would love any help of you guys to solve this problem. Thanks in advance

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
rezanahardanicommented, Jun 18, 2017
1reaction
xudshencommented, Oct 6, 2014

ok… try this:

step1: //add @Expose on every field to be deserialized @SerializedName(“id”) @Expose private Long mId; @Expose private String name;

step2: productResult.setResults((new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()).<ArrayList<Markets>>… tell Gson exclude fields without @Expose annotation.

the id field will be ignored since it has no annotation like @Expose

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sugar ORM is not saving data into the database - Stack Overflow
I am not able to save the data persistently into my database which is created by Sugar ORM. Here is the method, that...
Read more >
Android – Sugar ORM is not saving data into the database – iTecNote
I am not able to save the data persistently into my database which is created by Sugar ORM. Here is the method, that...
Read more >
Clean Persistence with Sugar ORM - CodePath Cliffnotes
Sugar ORM is a database persistence library that provides a simple and concise way to integrate your application models into SQLite.
Read more >
satyan/sugar - Gitter
Hi guys! I'm new to SugarORM and I just have one question. Does sugar automatically do rever foreign key lookups? What I mean...
Read more >
Sugar ORM: An easier way to work with SQLite Databases in ...
If you have been working with the Android SQLite database for some ... When using the sugar ORM, all data models or classes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found