Android-contentResolver.notifyChange()降低性能
我有一个MyContentProvider类,它覆盖Android-contentResolver.notifyChange()降低性能,android,performance,sqlite,android-contentprovider,android-contentresolver,Android,Performance,Sqlite,Android Contentprovider,Android Contentresolver,我有一个MyContentProvider类,它覆盖bulkInsert()。在这个方法中,我使用SQLite事务将大约4000行插入数据库,在三星Galaxy S4设备上大约需要25秒 但是,当我从我的bulkInsert()方法中删除这一行时 @Override public int bulkInsert(Uri uri, ContentValues[] valuesArray) { /* * Open a read / write database to su
bulkInsert()
。在这个方法中,我使用SQLite事务将大约4000行插入数据库,在三星Galaxy S4设备上大约需要25秒
但是,当我从我的bulkInsert()
方法中删除这一行时
@Override
public int bulkInsert(Uri uri, ContentValues[] valuesArray) {
/*
* Open a read / write database to support the transaction.
*/
SQLiteDatabase db = dbHelper.getWritableDatabase();
String tableName;
switch (uriMatcher.match(uri)) {
case BRANDS_SEARCH:
tableName = BRAND_NAMES_TABLE;
break;
case PRODUCTS_SEARCH:
tableName = PRODUCTS_TABLE;
break;
case PARENT_COMPANIES_SEARCH:
tableName = PARENT_COMPANIES_TABLE;
break;
case PRODUCTS_DATA_SEARCH:
tableName = PRODUCTS_DATA_TABLE;
break;
default:
//break;
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
/*
* Begin the transaction
*/
db.beginTransaction();
int numSuccessfulInsertions = 0;
try {
for (int i = 0; i < valuesArray.length; i++) {
/*
* Insert the values into the table
*/
long rowId = db.insert(tableName, null, valuesArray[i]);
if (rowId > -1) {
/*
* Increment numSuccessfulInsertions
*/
numSuccessfulInsertions++;
/*
* Construct the URI of the newly inserted row.
*/
Uri insertedId = ContentUris.withAppendedId(uri, rowId);
/*
* Notify any observers of the change in the data set.
*/
getContext().getContentResolver().notifyChange(insertedId, null);
}
else {
/*
* Don't give up (as not all insert attempts need to succeed)
*/
//throw new Exception("Could not insert row");
}
}
/*
* Return number of successful insertions
*/
return numSuccessfulInsertions;
}
catch(Exception e) {
Log.e(LOG_TAG, "bulkInsert exception", e);
/*
* Return number of successful insertions
*/
return numSuccessfulInsertions;
}
finally {
/*
* Some (or all) insertion attempts succeeded
*/
db.setTransactionSuccessful();
/*
* Always end the transaction
*/
db.endTransaction();
}
}
getContext().getContentResolver().notifyChange(insertedId,null)代码>
…总插入时间降至约1或2秒
那么,有没有更好的方法调用notifyChange()
我试着在另一个线程中调用它,就像这样
new Thread(new Runnable() {
public void run() {
getContext().getContentResolver().notifyChange(insertedId, null);
}
}).start();
…但它仍然很慢,并且由于某些原因,会导致OutOfMemoryError
为了完整起见,这里是我的bulkInsert()
方法
@Override
public int bulkInsert(Uri uri, ContentValues[] valuesArray) {
/*
* Open a read / write database to support the transaction.
*/
SQLiteDatabase db = dbHelper.getWritableDatabase();
String tableName;
switch (uriMatcher.match(uri)) {
case BRANDS_SEARCH:
tableName = BRAND_NAMES_TABLE;
break;
case PRODUCTS_SEARCH:
tableName = PRODUCTS_TABLE;
break;
case PARENT_COMPANIES_SEARCH:
tableName = PARENT_COMPANIES_TABLE;
break;
case PRODUCTS_DATA_SEARCH:
tableName = PRODUCTS_DATA_TABLE;
break;
default:
//break;
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
/*
* Begin the transaction
*/
db.beginTransaction();
int numSuccessfulInsertions = 0;
try {
for (int i = 0; i < valuesArray.length; i++) {
/*
* Insert the values into the table
*/
long rowId = db.insert(tableName, null, valuesArray[i]);
if (rowId > -1) {
/*
* Increment numSuccessfulInsertions
*/
numSuccessfulInsertions++;
/*
* Construct the URI of the newly inserted row.
*/
Uri insertedId = ContentUris.withAppendedId(uri, rowId);
/*
* Notify any observers of the change in the data set.
*/
getContext().getContentResolver().notifyChange(insertedId, null);
}
else {
/*
* Don't give up (as not all insert attempts need to succeed)
*/
//throw new Exception("Could not insert row");
}
}
/*
* Return number of successful insertions
*/
return numSuccessfulInsertions;
}
catch(Exception e) {
Log.e(LOG_TAG, "bulkInsert exception", e);
/*
* Return number of successful insertions
*/
return numSuccessfulInsertions;
}
finally {
/*
* Some (or all) insertion attempts succeeded
*/
db.setTransactionSuccessful();
/*
* Always end the transaction
*/
db.endTransaction();
}
}
@覆盖
public int bulkInsert(Uri,ContentValues[]value数组){
/*
*打开读/写数据库以支持事务。
*/
SQLiteDatabase db=dbHelper.getWritableDatabase();
字符串表名;
开关(uriMatcher.match(uri)){
案例品牌搜索:
tableName=品牌名称表;
打破
案例产品搜索:
tableName=产品\表格;
打破
案例母公司搜索:
tableName=母公司\表;
打破
案例产品\数据\搜索:
tableName=产品\数据\表格;
打破
违约:
//中断;
抛出新的IllegalArgumentException(“不支持的URI:+URI”);
}
/*
*开始交易
*/
db.beginTransaction();
int numSuccessfulInsertions=0;
试一试{
对于(int i=0;i-1){
/*
*增量numSuccessfulInsertions
*/
numSuccessfulInsertions++;
/*
*构造新插入行的URI。
*/
uriinsertedid=ContentUris.withAppendedId(Uri,rowId);
/*
*将数据集中的更改通知任何观察者。
*/
getContext().getContentResolver().notifyChange(insertedId,null);
}
否则{
/*
*不要放弃(因为并非所有插入尝试都需要成功)
*/
//抛出新异常(“无法插入行”);
}
}
/*
*返回成功插入的次数
*/
返回numsuccessfullinsertions;
}
捕获(例外e){
Log.e(Log_标签,“bulkInsert异常”,e);
/*
*返回成功插入的次数
*/
返回numsuccessfullinsertions;
}
最后{
/*
*部分(或全部)插入尝试成功
*/
db.setTransactionSuccessful();
/*
*始终结束交易
*/
db.endTransaction();
}
}
批量操作通知一次,而不是每次插入记录通知一次。将调用移动到notifyChange,使其遵循for循环。批量操作只通知一次,而不是每次插入记录都通知一次。将调用移动到notifyChange,使其跟随for循环。同时使用“dir-uri”而不是“item-uri”(not:content://host/table/1
但整个content://host/table
)为了应用解决方案,我在最后一个块的末尾添加了以下内容:if(numSuccessfulInsertions>0)getContext().getContentResolver().notifyChange(uri,null)代码>也用“dir uri”而不是“item uri”通知(不是:content://host/table/1
但整个content://host/table
)为了应用解决方案,我在最后一个块的末尾添加了以下内容:if(numSuccessfulInsertions>0)getContext().getContentResolver().notifyChange(uri,null)代码>