lundi 11 janvier 2016

Cursor.registercontentobserver vs ContentResolver.registercontentobserver

I am trying to use loaders without content provider.

To achieve this, while inserting/updating/deleting data from SQLite i use following code to notify.

getContext().getContentResolver().notifyChange(CONTENT_URI, null);

On the other hand, i have created one custom loader class to load data directly from SQLite and to get notify automatically when content changes.

public class SQLiteCursorLoader extends CursorLoader
{

    private String                          tableName;
    private String                          rawQuery    = null;
    private final ForceLoadContentObserver  mObserver   = new ForceLoadContentObserver();
    private String[]                        projection;
    private String                          selection;
    private String[]                        selectionArgs;
    private String                          sortOrder;
    Loader<Cursor>                          ld          = null;

    public SQLiteCursorLoader(Context context, String tableName, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    {
        super(context);
        this.tableName = tableName;
        this.projection = projection;
        this.selection = selection;
        this.selectionArgs = selectionArgs;
        this.sortOrder = sortOrder;
    }

    public SQLiteCursorLoader(Context context, String rawQuery)
    {
        super(context);
        this.rawQuery = rawQuery;
    }

    @Override
    public Cursor loadInBackground()
    {
        //super.loadInBackground();
        Cursor cursor = null;
        SQLiteDatabase db = AndroidSQLite.getInstatnce().getDatabase();
        try
        {
            if(rawQuery == null)
            {
                cursor = db.query(tableName, projection, selection, selectionArgs, null, null, sortOrder);
            }
            else
            {
                cursor = db.rawQuery(rawQuery, null);
            }
            // Ensure the cursor window is filled.
            //cursor.setNotificationUri(getContext().getContentResolver(), UserGroupCP.CONTENT_URI);
            cursor.getCount();
            //cursor.registerContentObserver(mObserver);
            getContext().getContentResolver().registerContentObserver(UserGroupCP.CONTENT_URI, true, mObserver);
        }
        catch(Exception e)
        {
            ErrorReportProvider.sendErrorMail(Helper.getStackTrace(e), TenoPhotoConstant.defaultDisplay);
        }
        return cursor;

    }

}

But whenever i try to replace

getContext().getContentResolver().registerContentObserver(UserGroupCP.CONTENT_URI, true, mObserver);

with

cursor.setNotificationUri(getContext().getContentResolver(), UserGroupCP.CONTENT_URI);
cursor.registerContentObserver(mObserver);

in my custom loader class (as in android.content.CursorLoader's loadInBackground() method), it does not get notify automatically at all.

i am not getting any clue about this. In android.content.CursorLoader class, this works fine but not in custom class extending CursorLoader class. Kindly explain this.

Also explain about significance and difference between these three lines of code.

cursor.setNotificationUri(getContext().getContentResolver(), UserGroupCP.CONTENT_URI);

cursor.registerContentObserver(mObserver);

getContext().getContentResolver().registerContentObserver(UserGroupCP.CONTENT_URI, true, mObserver);

Aucun commentaire:

Enregistrer un commentaire