Android sqlite begintransaction执行时间过长

Android sqlite begintransaction执行时间过长,第1张

概述我想将从json解析的数据批量插入到db中.我使用方法下面插入批处理.问题是mDbWritable.beginTransaction();执行时间太长.通常像6秒!我不知道问题出在哪里.有些想法会导致如此长的执行时间?非常感谢. @Overridepublic ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperatio 我想将从Json解析的数据批量插入到db中.我使用方法下面插入批处理.问题是mDbWritable.beginTransaction();执行时间太长.通常像6秒!我不知道问题出在哪里.有些想法会导致如此长的执行时间?非常感谢.

@OverrIDepublic 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;}

日志中的一些示例:

11-16 15:14:53.726: I/APIProvIDer(21442): Time applyBatch beginTransaction: 602511-16 15:15:00.713: I/APIProvIDer(21442): Time applyBatch beginTransaction: 494011-16 15:15:17.819: I/APIProvIDer(21442): Time applyBatch beginTransaction: 865111-16 15:15:45.346: I/APIProvIDer(21442): Time applyBatch beginTransaction: 1267211-16 15:16:16.807: I/APIProvIDer(21442): Time applyBatch beginTransaction: 1241111-16 15:16:45.685: I/APIProvIDer(21442): Time applyBatch beginTransaction: 1224711-16 15:17:01.500: I/APIProvIDer(21442): Time applyBatch beginTransaction: 12788

编辑:解析Json时我在循环中使用apply batch.例如对于Json中的每个项目 – 解析并应用批处理.批处理包含插入,更新,删除 *** 作.

以下是我如何迭代并调用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);        //execute update make applybatch call        executeUpdate(channelAPIUri,channelHandler);        starredChannelsCursor.movetoNext();    }}if (starredChannelsCursor != null) {    starredChannelsCursor.close();}/*** Make call to Uri,parse response and apply batch operations to* contentResolver* * @param APIUri* @param handler*            - handles parsing*/private boolean executeUpdate(URI APIUri,AbstractJsONHandler handler) {    APIResponse APIResponse = mhttpHelper.dohttpCall(APIUri);    ArrayList<ContentProvIDerOperation> batch =                    new ArrayList<ContentProvIDerOperation>();    if (APIResponse != null) {        batch = handler.parse(APIResponse);        Alog.v(TAG,"update user data from " + APIUri);    }    if (batch.size() > 0) {        try {            mContentResolver.applyBatch(APIContract.CONTENT_AUTHORITY,batch);        } catch (Exception e) {            Alog.v(TAG,"Error: " + e.getMessage());        }    }    return true;}
解决方法 似乎唯一可能的问题是,不同的线程在调用beginTransaction()时获取相同的锁,并且浪费时间等待其他线程释放锁.查看您的代码,了解如何管理线程以及从哪个线程调用applyBatch(..)方法.

SQLiteDatabase类中查看beginTransaction()的调用层次结构可能也很有用.

总结

以上是内存溢出为你收集整理的Android sqlite begintransaction执行时间过长全部内容,希望文章能够帮你解决Android sqlite begintransaction执行时间过长所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1126767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存