【发布时间】:2018-01-02 20:38:55
【问题描述】:
我需要围绕我没有创建但应该使用的两种方法创建一个内容提供程序事务。方法addDog(Context, Dog) throws Exception 在 Dog 表中添加一行。方法 addToy(Context,Toy, long dogId) throws Exception 在 Toy 表中添加一行。我想创建一个方法
public void addDogAndToyAtomic(Context context, Dog dog, Toy toy) throws Exception{
Transaction transaction = null;
try{
transaction = ContentProviders.getTransaction(...);//what is this?
transaction.start();
. . . //some more queries here and then
long dogId = addDog(context, dog);
addToy(context, toy, dogId);
transaction.commit();
}finally{
if(null != transaction)transaction.close();
}
}
如何在 Android 中创建此方法?我知道正在使用的提供程序的 CONTENT_URI,实际上我可以看到这两种方法的内部。这个问题的限制是我想使用现有的两种方法来创建第三个。
如果你好奇,addDog 看起来像这样:
public long addDog(Context context, Dog dog){
Uri uri= context.getContentResolver().insert(
PetContract.Dog.CONTENT_URI,
dog.getContentValues()
);
return ContentUris.parseId(uri);
}
【问题讨论】:
标签: android database sqlite transactions android-contentprovider