1

I'm using rushorm for sqlite object serializing and storage. It work pretty cool so far. The problem is that I wan't to store following Request object:

public class Request extends RushObject {

    private String url;
    private RequestParamsStorable params;

    public Request() {}

    public Request(String aUrl, RequestParamsStorable aParams)
    {
        this.url = aUrl;
        this.params = aParams;
    }

    public String getUrl()
    {
        return this.url;
    }

    public RequestParams getParams()
    {
        return this.params;
    }

}

As you can see I need to store RequestParams object. In order to store it, as I obviously cannot make it to extend RushObject I made a subclass and made it to implement Rush as per docs instructions:

public class RequestParamsStorable extends RequestParams implements Rush {

    public RequestParamsStorable() {}

    @Override
    public void save() { RushCore.getInstance().save(this); }
    @Override
    public void save(RushCallback callback) { RushCore.getInstance().save(this, callback); }
    @Override
    public void delete() { RushCore.getInstance().delete(this); }
    @Override
    public void delete(RushCallback callback) { RushCore.getInstance().delete(this, callback); }
    @Override
    public String getId() { return RushCore.getInstance().getId(this); }
}

It didn't throw any errors and calling save() on Request object went smoothly. When I ask for stored objects like that:

    List<Request> remainingsInDB = new RushSearch().find(Request.class);

I indeed receive stored Request objects, with proper url, however RequestParamsStorable is empty(""). I checked and when I save them, they definetely had values, and are not empty.

So the question is where I'm wrong?

Regards,

hris.to
  • 6,235
  • 3
  • 46
  • 55
  • 1
    Is the RequestParams object your are extending this? https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/RequestParams.java – Stuart Campbell Sep 30 '15 at 19:29
  • Yes it is. I understand the `final` problem and trying to make a workaround with JSONObject. – hris.to Oct 01 '15 at 12:15

2 Answers2

1

If your parent class RequestParams contains fields declared as final, they will not be restored.

As reference you can see the RushORM source code

ReflectionClassLoader.java

    for (Field field : fields) {
        field.setAccessible(true);
        if (!annotationCache.get(clazz).getFieldToIgnore().contains(field.getName())) {
            if (!loadJoinField(object, rushColumns, annotationCache, field, joins, joinTables)) {
                if(rushColumns.supportsField(field)) {
                    String value = values.get(counter);
                    if(value != null && !value.equals("null")) {
                        rushColumns.setField(object, field, value);
                    }
                    counter++;
                }
            }
        }
    }

You should either remove final modifier of the fields or create wrapper object which will be stored in the db and then restore RequestParams from it

petod
  • 96
  • 1
  • 1
0

Okay the problem indeed is fields declared as final in RequestParams. @Stuart Campbell properly noted RequestParams reference. Now what I'm trying as a workaround is to store all properties(except Wrappers) as JSONObject, store this JSONObject as String and then restore state from it. I'm facing some issues with JsonWritter and didn't solved my issue yet. Not sure if it is a good idea to post relevant code here, or to post new question though?

hris.to
  • 6,235
  • 3
  • 46
  • 55
  • Have a look at this http://www.rushorm.com/advanced.html#Customise You could create a custom column type for RequestParams and do you serialization and deserialization there. – Stuart Campbell Oct 01 '15 at 13:02