问题:源码交易时使用ContentResolver
题
我们的目标:刷新的数据库,从XML数据
过程:
- 启动交易
- 删除 所有现有的行表
- 每个主要因素的分析XML 插入 列入主要表和获得PK
- 每个儿童的主要元素 插入 记录到2表提供FK从前一步
- 提交事务
漂亮的标准的东西尽db行动。问题是,污物的操作都不在 ContentProvider
而是使用 ContentResolver
所以插入例如看起来像 resolver.insert(CONTENT_URI, contentValues)
.该ContentResolver API似乎没有任何涉及的交易而我也不能使用 bulkInsert
因为我是插入在2间歇地表(加上我想要 delete
内部事务)。
我想登记我定制的 ContentProvider
作为听众通过使用 registerContentObserver
但是,因为 ContentResolver#acquireProvider
方法都是隐藏我如何获得正确的参考?
我的运气了?
解决方案
我已经看到,在对源代码的谷歌I/O应用程序,它们复盖 ContentProvider
's applyBatch()
方法和使用的交易。所以,你创建的一批 ContentProviderOperation
s然后叫 getContentResolver().applyBatch(uri_authority, batch)
.
我计划使用这种方法来看看它是如何工作的。我很好奇如果任何人试图通过它。
其他提示
这是可以做到的基于交易的多表中插入相当干净,因为安卓2.1通过使用ContentProviderOperation,如前所述通过kaciula.
当你建立的ContentProviderOperation目的,你可以打电话。withValueBackReference(fieldName,refNr).操作时使用applyBatch,结果是,ContentValues的对象是供插入()呼叫会有一个整数注射。整将输入的名字符串,其价值是从ContentProviderResult以前施加的ContentProviderOperation、编入索引的通过refNr.
请参考代码样本如下。在本示例中,插入行在table1,并得到的身份证(在这种情况下的"1"),然后用作为一个值当插入列在表2中。为简洁起见,本点的内容提供器是不相连的数据库。在这点的内容提供器,有打印输出,这将是合适的,添加该事务处理。
public class BatchTestActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<ContentProviderOperation> list = new
ArrayList<ContentProviderOperation>();
list.add(ContentProviderOperation.
newInsert(BatchContentProvider.FIRST_URI).build());
ContentValues cv = new ContentValues();
cv.put("name", "second_name");
cv.put("refId", 23);
// In this example, "refId" in the contentValues will be overwritten by
// the result from the first insert operation, indexed by 0
list.add(ContentProviderOperation.
newInsert(BatchContentProvider.SECOND_URI).
withValues(cv).withValueBackReference("refId", 0).build());
try {
getContentResolver().applyBatch(
BatchContentProvider.AUTHORITY, list);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
}
public class BatchContentProvider extends ContentProvider {
private static final String SCHEME = "content://";
public static final String AUTHORITY = "com.test.batch";
public static final Uri FIRST_URI =
Uri.parse(SCHEME + AUTHORITY + "/" + "table1");
public static final Uri SECOND_URI =
Uri.parse(SCHEME + AUTHORITY + "/" + "table2");
public ContentProviderResult[] applyBatch(
ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
System.out.println("starting transaction");
ContentProviderResult[] result;
try {
result = super.applyBatch(operations);
} catch (OperationApplicationException e) {
System.out.println("aborting transaction");
throw e;
}
System.out.println("ending transaction");
return result;
}
public Uri insert(Uri uri, ContentValues values) {
// this printout will have a proper value when
// the second operation is applied
System.out.println("" + values);
return ContentUris.withAppendedId(uri, 1);
}
// other overrides omitted for brevity
}
所有的权利-所以这不会丁漫无目的:我唯一能想到的是代码startTransaction和endTransaction作为基于URL的查询请求。喜欢的东西 ContentResolver.query(START_TRANSACTION, null, null, null, null)
.然后在 ContentProvider#query
基于登记的URL通话开始或结束交易
你可以得到执行的内容提供商对象本身(如果在相同的过程中,提示:你可以控制的供应商的过程中与多进="真正的"或处理="" http://developer.android.com/guide/topics/manifest/provider-element.html)使用ContentProviderClient.getLocalContentProvider()它可能转换为你提供者实现其可以提供额外的功能,像一个reset()关闭和删除的数据库,也可以返回一个自定义的交易的类实例有保存()and close()方法。
public class Transaction {
protected Transaction (SQLiteDatabase database) {
this.database = database;
database.beginTransaction ();
}
public void save () {
this.database.setTransactionSuccessful ();
}
public void close () {
this.database.endTransaction ();
}
private SQLiteDatabase database;
}
public Transaction createTransaction () {
return new Transaction (this.dbHelper.getWritableDatabase ());
}
然后:
ContentProviderClient client = getContentResolver ().acquireContentProviderClient (Contract.authorityLocal);
Transaction tx = ((LocalContentProvider) client.getLocalContentProvider ()).createTransaction ();