I want to insert parsed data from json to db in batch mode. I use the method below to insert a package. The problem is that mDbWritable.beginTransaction (); takes too long to complete. Usually like 6 seconds! I do not know where the problem is. Some thoughts, what can cause such a long execution time? Thank you very much.
@Override public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { long start = System.currentTimeMillis(); mDbWritable.beginTransaction(); long time = System.currentTimeMillis() - start; Alog.i(TAG, "Time applyBatch beginTransaction: " + time); final int numOperations = operations.size(); final ContentProviderResult[] results = new ContentProviderResult[numOperations]; try { for (int i = 0; i < numOperations; i++) { results[i] = operations.get(i).apply(this, results, i); } mDbWritable.setTransactionSuccessful(); } finally { mDbWritable.endTransaction(); } return results; }
Example from magazines:
11-16 15:14:53.726: I/ApiProvider(21442): Time applyBatch beginTransaction: 6025 11-16 15:15:00.713: I/ApiProvider(21442): Time applyBatch beginTransaction: 4940 11-16 15:15:17.819: I/ApiProvider(21442): Time applyBatch beginTransaction: 8651 11-16 15:15:45.346: I/ApiProvider(21442): Time applyBatch beginTransaction: 12672 11-16 15:16:16.807: I/ApiProvider(21442): Time applyBatch beginTransaction: 12411 11-16 15:16:45.685: I/ApiProvider(21442): Time applyBatch beginTransaction: 12247 11-16 15:17:01.500: I/ApiProvider(21442): Time applyBatch beginTransaction: 12788
EDIT: I use a batch loop when parsing json. for example for each item in json-parse and apply the package. The package contains insert, update, delete operations.
Here is the code as I repeat and call applyBatch
Cursor starredChannelsCursor = mContentResolver.query(ApiContract.Channels.CONTENT_URI, new String[] {BaseColumns._ID, ChannelsTable.ID, ChannelsTable.SLUG }, ChannelsTable.IS_STARRED + "=?",new String[] { "1" }, null); String userName = mSettings.getUserName(); if (starredChannelsCursor != null && starredChannelsCursor.moveToFirst()) { while (!starredChannelsCursor.isAfterLast()) { String channelSlug = starredChannelsCursor.getString(2); ChannelHandler channelHandler = new ChannelHandler(this); URI channelApiUri = Constants.getChannelApiURI(channelSlug,userName);
source share