最近客户反馈一个问题,说节目量很多的时候(超过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;
}
到此,大功告成!!!