简体   繁体   English

Sugar ORM不会将数据保存到数据库中

[英]Sugar ORM is not saving data into the database

I am currently using Sugar ORM and Android Async Http Client for my Android application. 我目前正在为我的Android应用程序使用Sugar ORMAndroid Async Http Client

I read through the documentation of Sugar ORM and did exactly what is written there. 我阅读了Sugar ORM的文档并完成了那里写的内容。 My HttpClient is using the singleton pattern and provides methods for calling some APIs. 我的HttpClient使用单例模式并提供调用某些API的方法。

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. 我无法将数据持久保存到由Sugar ORM创建的数据库中。 Here is the method, that is calling an API: 这是调用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 is printing: 这是Logcat打印的内容:

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 here at stackoverflow, but no answers or questions could give me a hint on how to solve that problem. 此外,我在stackoverflow看了一下Sugar ORM标签,但没有答案或问题可以给我一个如何解决这个问题的提示。 I am a newbie to the android ecosystem and would love any help of you guys to solve this problem. 我是android生态系统的新手,很乐意帮助你解决这个问题。 Thanks in advance 提前致谢

I just solve it the same problem as you have. 我只是解决了你遇到的同样问题。 It was a pain in the neck but after few hours I find out what caused this problem. 这是一个痛苦的脖子,但几个小时后,我发现是什么导致了这个问题。

Using Sugar ORM you must not set id property as it's belongs to SugarRecord class, otherwise ORM will try to update objects instead of insert them. 使用Sugar ORM,您不能设置id属性,因为它属于SugarRecord类,否则ORM将尝试更新对象而不是插入它们。 As I need to have field with my object id, I used json annotation to assign it to another field. 因为我需要有我的对象id的字段,我使用json注释将它分配给另一个字段。 Last step was configure GSON to exclude fields without Expose annotation. 最后一步是配置GSON以排除没有Expose注释的字段。

So my class looks like one below now: 所以我的班级现在看起来像下面一个:

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  

}

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM