Skip to content

Commit

Permalink
Replace deprecated beginTransaction, etc. with runInTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhanniballake committed May 15, 2019
1 parent 8ba1bab commit dea3a83
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ class RepoDaoTest : DbTest() {
val repo = TestUtil.createRepo("foo", "bar", "desc")
val c1 = TestUtil.createContributor(repo, "c1", 3)
val c2 = TestUtil.createContributor(repo, "c2", 7)
db.beginTransaction()
try {
db.runInTransaction {
db.repoDao().insert(repo)
db.repoDao().insertContributors(arrayListOf(c1, c2))
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}
val list = getValue(db.repoDao().loadContributors("foo", "bar"))
assertThat(list.size, `is`(2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@ class FetchNextSearchPageTask constructor(
query, ids,
apiResponse.body.total, apiResponse.nextPage
)
try {
db.beginTransaction()
db.runInTransaction {
db.repoDao().insert(merged)
db.repoDao().insertRepos(apiResponse.body.items)
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}
Resource.success(apiResponse.nextPage != null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,9 @@ class RepoRepository @Inject constructor(
totalCount = item.total,
next = item.nextPage
)
db.beginTransaction()
try {
db.runInTransaction {
repoDao.insertRepos(item.items)
repoDao.insert(repoSearchResult)
db.setTransactionSuccessful()
} finally {
db.endTransaction()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FetchNextSearchPageTaskTest {
fun init() {
service = mock(GithubService::class.java)
db = mock(GithubDb::class.java)
`when`(db.runInTransaction(any())).thenCallRealMethod()
repoDao = mock(RepoDao::class.java)
`when`(db.repoDao()).thenReturn(repoDao)
task = FetchNextSearchPageTask("foo", service, db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ public static void switchToInMemory(Context context) {
*/
private void populateInitialData() {
if (cheese().count() == 0) {
Cheese cheese = new Cheese();
beginTransaction();
try {
for (int i = 0; i < Cheese.CHEESES.length; i++) {
cheese.name = Cheese.CHEESES[i];
cheese().insert(cheese);
runInTransaction(new Runnable() {
@Override
public void run() {
Cheese cheese = new Cheese();
for (int i = 0; i < Cheese.CHEESES.length; i++) {
cheese.name = Cheese.CHEESES[i];
cheese().insert(cheese);
}
}
setTransactionSuccessful();
} finally {
endTransaction();
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.example.android.contentprovidersample.data.CheeseDao;
import com.example.android.contentprovidersample.data.SampleDatabase;
import java.util.ArrayList;
import java.util.concurrent.Callable;


/**
Expand Down Expand Up @@ -167,24 +168,23 @@ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable St
}
}

@SuppressWarnings("RedundantThrows") /* This gets propagated up from the Callable */
@NonNull
@Override
public ContentProviderResult[] applyBatch(
@NonNull ArrayList<ContentProviderOperation> operations)
@NonNull final ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
final Context context = getContext();
if (context == null) {
return new ContentProviderResult[0];
}
final SampleDatabase database = SampleDatabase.getInstance(context);
database.beginTransaction();
try {
final ContentProviderResult[] result = super.applyBatch(operations);
database.setTransactionSuccessful();
return result;
} finally {
database.endTransaction();
}
return database.runInTransaction(new Callable<ContentProviderResult[]>() {
@Override
public ContentProviderResult[] call() throws OperationApplicationException {
return SampleContentProvider.super.applyBatch(operations);
}
});
}

@Override
Expand Down

0 comments on commit dea3a83

Please sign in to comment.