android里更新数据量比较大时的批量处理方法

最近客户反馈一个问题,说节目量很多的时候(超过1万),移动节目位置后保存的时候发的时间太久了。我测试了下,确实要很久,大概1分钟。查看代码,发现更新数据时是一条一条更新的,下面的代码就被执行了1万多下,从而导致很耗时间。

ContentValues srvValues = new ContentValues();

srvValues.put("chan_order", item.getServiceIndex());
srvValues.put("chan_num", item.getServiceIndex());
srvValues.put("default_chan_num", item.getServiceIndex());
getContentResolver().update(DVBClient.TABLE_SERVICE, srvValues,"db_id=" + item.getServiceDbId(), null);

后来在网上查阅,发现有批处理的方法,而且按照该方法,数据更新时间减少到了10秒左右,神奇啊!

更新数据批处理方法主要就是调用ContentProviderOperation类的newUpdate方法和ContentResolver类的applyBatch方法。

我的代码如下:

 

 

ContentResolver resolver = getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

if (allChannelInfos != null && allChannelInfos.size() > 0) {
   for(i = 0; i<allChannelInfos.size(); i++) {
      item = allChannelInfos.get(i);
      if (item.getServiceIndex() != item.getServiceNumb()) {
         if (isMoveFav) {
            ContentValues srvValues = new ContentValues();
            srvValues.put("gm_reserveInt1", item.getServiceIndex());
            ContentProviderOperation operation = ContentProviderOperation.newUpdate(DVBClient.TABLE_GRP_MAP)
                  .withSelection("db_srv_id=?"  + " and db_grp_id=?", new String[]{String.valueOf(item.getServiceDbId()), String.valueOf(grp_id)})
                  .withValues(srvValues)
                  .withYieldAllowed(true)
                  .build();
            operations.add(operation);
         } else {
            ContentValues srvValues = new ContentValues();
            srvValues.put("chan_order", item.getServiceIndex());
            srvValues.put("chan_num", item.getServiceIndex());
            srvValues.put("default_chan_num", item.getServiceIndex());
            ContentProviderOperation operation = ContentProviderOperation.newUpdate(DVBClient.TABLE_SERVICE)
                  .withSelection("db_id=?", new String[]{String.valueOf(item.getServiceDbId())})
                  .withValues(srvValues)
                  .withYieldAllowed(true)
                  .build();
            operations.add(operation);
         }
      }

      if(operations.size() == 1000) {
         try {
            ContentProviderResult[]results = resolver.applyBatch("com.amlogic.dvb.DVBDataProvider", operations);
            operations.clear();
         }  catch (RemoteException e) {
            Log.i(TAG,"catch RemoteException");
         } catch (OperationApplicationException e) {
            Log.i(TAG,"catch OperationApplicationException");
         }
      }
   }

   if(operations.size() > 0) {
      try {
         ContentProviderResult[]results = resolver.applyBatch("com.amlogic.dvb.DVBDataProvider", operations);
         operations.clear();
         //DLog.d("wujiang", "count = " + results.length);
         ischange = true;
      }  catch (RemoteException e) {
         Log.i(TAG,"catch RemoteException");
      } catch (OperationApplicationException e) {
         Log.i(TAG,"catch OperationApplicationException");
      }
   }
}

 

 

 

applyBatch第一个参数是内容提供器的名称,第二个参数是要更新数据的集合。

我这里用if(operations.size() == 1000)判断下,是不想给内容提供那边传太多的数据,因为传的数据太大会报RemoteException的异常。

 

实现批处理还有个地方必须注意的就是一定要在内容提供器那边实现applyBatch方法,如下:

 

@Override
     public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation>operations) throws OperationApplicationException {
               SQLiteDatabase db = mOpenHelper.getWritableDatabase();
               db.beginTransaction();
               try{
                   ContentProviderResult[]results = super.applyBatch(operations);
                   db.setTransactionSuccessful();
                   return results;
               } finally {
                   db.endTransaction();
              }

              return null;
     }

到此,大功告成!!!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值