ormlite-android-extras icon indicating copy to clipboard operation
ormlite-android-extras copied to clipboard

Patch for OrmliteCursorLoader for Android 2.2+

Open PerfectCarl opened this issue 12 years ago • 0 comments

I was trying to use to with Android 2.2 and I had to patch the loadInBackground for the class OrmliteCursorLoader<T> replacing that code :

    /* Runs on a worker thread */
    @Override
    public Cursor loadInBackground() {
        Cursor cursor = null;
        try
        {
            cursor = mDao.getCursor(mQuery);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        if (cursor != null) {
            // Ensure the cursor window is filled
            cursor.getCount();
            registerContentObserver(cursor, mObserver);
        }
        return cursor;
    }

with

    /* Runs on a worker thread */
    @Override
    public Cursor loadInBackground() {
        Cursor cursor = null;
        CloseableIterator<T> iterator = null;
        try {
            iterator = mDao.iterator(mQuery);

            // get the raw results which can be cast under Android
            AndroidDatabaseResults results = (AndroidDatabaseResults) iterator
                    .getRawResults();
            cursor = results.getRawCursor();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // FIXME : is this normal ??
            // if (iterator != null)
            // iterator.closeQuietly();
        }
        if (cursor != null) {
            // Ensure the cursor window is filled
            cursor.getCount();
            registerContentObserver(cursor, mObserver);
        }
        return cursor;
    }

I noticed that you can't call closeQuietly() as suggested in the semi official ormlite documentation because it crashes. I have no time for the moment to investigate is this is normal or not.

Anyway, this is my hasty contribution : it does work for Android 2.2+, I haven't checked the memory usage yet.

PerfectCarl avatar Jan 22 '13 18:01 PerfectCarl