0
votes

I have a fragment with a list of items, each item has an editable quantity. When I edit the quantity, the database is updated and I receive a new cursor in the fragment. This all works well, except when you update the quantity a number of times, the onLoadFinished method is not called anymore. This can happen after 5 updates or after 200 updates: I can't reproduce the bug on command. The update is executed in the database because when I close and open that screen, I can see the updated quantity. Nothing out of the ordinary happens in the code: onLoaderReset is not called, notifyChange gets called on the correct Uri in the contentprovider. Has anyone ever seen this issue?

When the quantity needs to get updated:

        ContentValues cv = new ContentValues();
        cv.put(CartTable.COLUMN_COUNT, quantity);
        String selection = CartTable.COLUMN_ID + " = " + cartItemId;
        contentResolver.update(CartContentProvider.CONTENT_URI, cv, selection, null);

The CartContentProvider:

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
    int rowsUpdated;

    switch (uriMatcher.match(uri))
    {
        case CART_ITEMS:
            rowsUpdated = this.database.update(CartTable.TABLE_NAME, values, selection, selectionArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    if(rowsUpdated > 0)
    {
        this.getContext().getContentResolver().notifyChange(CONTENT_URI, null);
        this.getContext().getContentResolver().notifyChange(CONTENT_URI_PRODUCTS, null);
        this.getContext().getContentResolver().notifyChange(CONTENT_URI_PRODUCTS_PHOTOS, null);
        this.getContext().getContentResolver().notifyChange(CONTENT_URI_PHOTOS, null);
    }
    return rowsUpdated;
}

When I create the CursorLoader:

return new CursorLoader(this.getView().getContext(), CartContentProvider.CONTENT_URI_PRODUCTS, projection, null, null, null);

So for example, I edit the quantity 20 times and 20 times onLoadFinished gets called with a new cursor. The 21st time and all times after that, the update happens but onLoadFinished is not called anymore.

When extra code is required, let me know!

Thanks in advance.

1

1 Answers

0
votes

Try using the CursorLoader like this:

return new CursorLoader(this.getActivity(), CartContentProvider.CONTENT_URI_PRODUCTS, projection, null, null, null);

use the fragment's Activity context, Also check where are you implementing the LoaderCallbacks interface, in the activity or in the fragment?