I need to create a Content Provider transaction around two methods that I didn't create but should use. Method addDog(Context, Dog) throws Exception
adds a row in the Dog table. Method addToy(Context,Toy, long dogId) throws Exception
adds a row in the Toy table. I would like to create a method
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();
}
}
How do I create this method in Android? I know the CONTENT_URI for the provider being used, in fact I can see inside the two methods. The constraint in this question is that I want to use the existing two methods to create this third.
In case you are curious, addDog
looks like this:
public long addDog(Context context, Dog dog){
Uri uri= context.getContentResolver().insert(
PetContract.Dog.CONTENT_URI,
dog.getContentValues()
);
return ContentUris.parseId(uri);
}