Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android sqlite begintransaction执行时间过长大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将从json解析的数据批量插入到db中.我使用方法下面插入批处理.问题是mDbWritable.begintransaction();执行时间太长.通常像6秒!我不知道问题出在哪里.有些想法会导致如此长的执行时间?非常感谢.

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

日志中的一些示例:

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

编辑:解析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执行时间过长所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。