2

This question would be a follow up from the one asked Content Resolver vs Cursor Loader Where the answer clearly states how to insert records into a sqlite Db using a Content Resolver . My question is the following :

  1. Can i use a loader(Normal Loader) to implement this functionality ?

eg :

    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
            switch (id) {

    case AppConstants.LOADER_ADD_FAV_PHONE_NUM:

        ContentValues values = new ContentValues();
        values.put(DBHelper.TM_DB_COLUMN_PHONE_NUMBER,
                directionOrder);
        values.put(TransitMeDBHelper.TM_DB_COLUMN_NAME_ID, selectionArgs[0]);
        Uri insertFavStop =getContentResolver().insert(TransitMeContentProvider.CONTENT_URI_INSERT_FAV_PHONE,
                        values);
        break;
    }

return null;
}

Would this run the functionality in the Main UI thread or a worker Thread . I understand that i would not receive callbacks for this as the content resolver will return a URI instead of the cursor containing data .

I tried running this code but it behaves kind of wierd .. Duplicate inserts are recorded and its not consistient , Could someone show me to some right resource which uses a Loader to do Insert operations ( I have seen many examples with query examples and it works like a charm :) )

Thank you for your time .

Community
  • 1
  • 1
Chris
  • 798
  • 1
  • 9
  • 15

1 Answers1

5

Can i use a loader(Normal Loader) to implement this functionality ?

You can't use a default CursorLoader to insert data. If you implement your own Loader you could probably use the loadInBackground method to do the insert but that will make your code awkward at best(and I don't know if it will actually work).

Would this run the functionality in the Main UI thread or a worker Thread . I understand that i would not receive callbacks for this as the content resolver will return a URI instead of the cursor containing data .

The loader callbacks will run on the thread where they are implemented(the UI main thread most likely) so you're doing the getContentResolver().insert() on the main UI thread with the possibility of blocking the main UI thread.

I tried running this code but it behaves kind of wierd .. Duplicate inserts are recorded and its not consistient ,

Have you tested how many times is the onCreateLoader method called?

Could someone show me to some right resource which uses a Loader to do Insert operations ( I have seen many examples with query examples and it works like a charm :) )

Don't use/try to use a Loader to insert data in the ContentProvider/Database. It works like a charm for queries because the loaders were designed to load data from a data source(not insert data), that is their purpose. If you want to insert data use an AsyncTask(also have a look at AsyncQueryhandler), a regular thread.

user
  • 86,916
  • 18
  • 197
  • 190
  • Hi thanks for the reply .. Yes i tried running with the normal cursor loader but returning null instead .. and it calls the onCreateLoader twice .. ( Also i noted that if i restart a loader it would crash since im returning null the previous call ) . I did not want to use the Async Task because it would react to config changes . Thank you for pointing me to the AsyncQueryHandler .. Then ideally is a cursor loader used only for Querying the data and none of the other C-U-D operations ? – Chris Nov 26 '12 at 20:28
  • @Chris *Then ideally is a cursor loader used only for Querying the data and none of the other C-U-D operations ?* Yes, use a `Loader` only to retrieve data from the provider/database or whatever data store you have. If I'm not mistaken, you can't return `null` from `onCreateLoader` so your code will fail. Also the `LoaderManager` takes care of the `Loader` data(for example the `Cursor`) to keep it in a valid state across configuration changes or data updates so those callbacks methods could be called more times then you want. – user Nov 26 '12 at 20:45
  • Thank you and i am assuming since the AsyncQueryHandler is extended from the Handler class it is not susceptible to config changes like a normal AysncTask or a Thread . This just makes the code little harder to maintain . But thank you for your response .. Im going to implement AsyncQueryhandler as per your advice . – Chris Nov 26 '12 at 20:58