gpt4 book ai didi

Android sqlite begintransaction 执行时间过长

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:27 26 4
gpt4 key购买 nike

我想把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(..) 方法。

查看位于 SQLiteDatabasebeginTransaction() 的调用层次结构可能对您也很有用。类。

关于Android sqlite begintransaction 执行时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13418569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com