DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Load JSONModel out of memory

Open OptimistNot opened this issue 11 years ago • 10 comments

Thank you for attention! Please help me, anytime i try to load() any model it gives me "Throwing OutOfMemoryError "Failed to allocate a 174 byte allocation with 136 free bytes and 136B until OOM" (recursive case)". And nope, im not calling it in recursive function... It just enters the load() and never goes out. ex: JSONModel json = new JSONModel(FinalProductAudit.class); Object[] o = {"1"}; //and i make sure this key exists in FinalProductAudit! json.load(o);

any ideia what is going on? Thank you!

OptimistNot avatar Apr 09 '15 18:04 OptimistNot

@OptimistNot good find. its calling itself recursively. Good find.

agrosner avatar Apr 11 '15 02:04 agrosner

Any ideia how long will it take you to fix it? Really need it to work fast. Thanks!

OptimistNot avatar Apr 12 '15 21:04 OptimistNot

@OptimistNot I apologize. I would subclass JsonModel for now (not ideal but works) and put in this patch:


/**
     * Loads a model from the DB into the json stored in this class. It also will recreate the JSON stored in this object
     *
     * @param primaryKeys The keys to reference (in order of the {@link ModelAdapter#getPrimaryModelWhere(Model)})
     */
    public void load(Object... primaryKeys) {
        setData(new JSONObject());
        Cursor cursor = new Select().from(mModelAdapter.getModelClass())
                .where(FlowManager.getPrimaryWhereQuery(getTable()).replaceEmptyParams(primaryKeys)).query();
        mContainerAdapter.loadFromCursor(cursor, this);
        cursor.close();
    }

agrosner avatar Apr 14 '15 18:04 agrosner

also you'll need to place the class in the same package as JSONModel

agrosner avatar Apr 14 '15 18:04 agrosner

oh yeah and don't forget to close cursor

agrosner avatar Apr 14 '15 18:04 agrosner

Thnak you a lot! Just one more question :) i really don't know what to do now with mContainerAdapter how do i get it? and how i get mModelAdapter?

OptimistNot avatar Apr 15 '15 11:04 OptimistNot

ok, i suppose its something like this: public JsonModel(Class table) { super(table); mModelAdapter = FlowManager.getModelAdapter(table); mContainerAdapter = FlowManager.getContainerAdapter(table); } wrong?

OptimistNot avatar Apr 15 '15 11:04 OptimistNot

hey! still same scenario, im sending one key and getting this one: "W/System.err﹕ android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1"

Log.d("CURSOR_____", cursor.getCount() + " " + cursor.getColumnNames().length + " " + cursor.getColumnIndex("final_product_audit_id")); gives me: D/CURSOR_____﹕ 1 21 0 so i make sure its not empty and i have all columns name.

on execute mContainerAdapter.loadFromCursor(cursor, this); catching in these lines: if (cursor.isNull(indexfinal_product_audit_id)) { modelContainer.put("final_product_audit_id",null); } else { modelContainer.put("final_product_audit_id",cursor.getLong(indexfinal_product_audit_id)); }

OptimistNot avatar Apr 15 '15 14:04 OptimistNot

found it! on isNull() it throws exception cz of not being at any position. so just before calling mContainerAdapter.loadFromCursor(cursor, this); added cursor.moveToFirst(); finally it loads data! Thank you very much! works perfectly!

OptimistNot avatar Apr 15 '15 15:04 OptimistNot

You're welcome. We will keep this open until fixed in 1.8.1

agrosner avatar Apr 15 '15 15:04 agrosner