Diskdrive
Diskdrive

Reputation: 18835

Updating column in onLoadFinished causing my query to run infinitely

At the moment I have a query that finds the top row in a table. This query is called using a Loader in the OnCreateLoader method.

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // TODO Auto-generated method stub

    int age = args.getInt("age");
    int yearDay = args.getInt("year");

    String sort = "(CASE WHEN DateSeen IS NULL "
            + "THEN 1 ELSE 0 END) DESC," + "DateSeen DESC," + "Abs((" + age
            + "  * 365) - (Age * 365) + (" + yearDay
            + " - (Case When YearDay is null then " + yearDay
            + "- 50 ELSE Yearday END)))";

    String[] projection = new String[] { DatabaseProvider.KEY_DESCRIPTION,
            DatabaseProvider.KEY_INT_AGE, DatabaseProvider.KEY_INT_YEARDAY,
            DatabaseProvider.KEY_ID };

    return new CursorLoader(this, DatabaseProvider.CONTENT_URI, projection,
            null, null, sort);

}

In the onLoadFinished method, I render the results on the screen but I also update a DateSeen column in the row (so that this row doesn't appear again if I refresh).

    @Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {

    // lots of code to display results in UI

    // update SeenDate
    c.moveToFirst();

    String Id = c.getString(c.getColumnIndex(DatabaseProvider.KEY_ID));
    ContentResolverCaller.UpdateDateSeen(Id);
}

My problem (at least I'm speculating) is that when the DateSeen column for the result is updated, because the Loader has a Content Observer to autoupdate if the data changes, and this is causing the query to be re-run and it's going into an infinite loop.

My question is is it possible to turn off the autoupdating feature of the loader? How can I udpate the DateSeen column without triggering the Loader to run the query again?

EDIT : I forgot to include the UpdateDateSeen method just in case the problem is there. It is below (I have a class where I store all my ContentResolver calls, not sure if it's the best solution - just out of neatness)

    public static int UpdateDateSeen(String Id) {
    ContentValues cv = new ContentValues();
    cv.put(DatabaseProvider.KEY_DATESEEN, new Date().getTime());

    return context.getContentResolver().update(
            Uri.withAppendedPath(DatabaseProvider.CONTENT_URI, Id), cv,
            null, null);
}

Upvotes: 2

Views: 397

Answers (1)

Tarun
Tarun

Reputation: 13808

I assume you are calling setNotificationUri in database helper class to watch for any changes in the db. You should not call

getContentResolver().notifyChange(uri, null);

in your update query. This will not trigger the content resolver to update the query again.

Upvotes: 1

Related Questions