From: Damyan Ivanov Date: Fri, 27 Aug 2021 20:07:31 +0000 (+0000) Subject: move async DB stuff away of AsyncTask X-Git-Tag: v0.20.1~15 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=860e0419d619210bb59bcae1b64d8c5db90eef70;p=mobile-ledger.git move async DB stuff away of AsyncTask it is deprecated the replacement is a single-thread executor instance in theory a multi-thread executor may work too, but there should not be so much concurrency on the DB level. getting the DB stuff away from the main thread should be enough --- diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java index b2d938c5..905bf23a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/BaseDAO.java @@ -17,19 +17,24 @@ package net.ktnx.mobileledger.dao; -import android.os.AsyncTask; - import androidx.annotation.NonNull; import net.ktnx.mobileledger.utils.Misc; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + public abstract class BaseDAO { + private final static Executor asyncRunner = Executors.newSingleThreadExecutor(); + public static void runAsync(Runnable runnable) { + asyncRunner.execute(runnable); + } abstract long insertSync(T item); public void insert(T item) { - AsyncTask.execute(() -> insertSync(item)); + asyncRunner.execute(() -> insertSync(item)); } public void insert(T item, @NonNull OnInsertedReceiver receiver) { - AsyncTask.execute(() -> { + asyncRunner.execute(() -> { long id = insertSync(item); Misc.onMainThread(() -> receiver.onInsert(id)); }); @@ -37,20 +42,20 @@ public abstract class BaseDAO { abstract void updateSync(T item); public void update(T item) { - AsyncTask.execute(() -> updateSync(item)); + asyncRunner.execute(() -> updateSync(item)); } public void update(T item, @NonNull Runnable onDone) { - AsyncTask.execute(() -> { + asyncRunner.execute(() -> { updateSync(item); Misc.onMainThread(onDone); }); } abstract void deleteSync(T item); public void delete(T item) { - AsyncTask.execute(() -> deleteSync(item)); + asyncRunner.execute(() -> deleteSync(item)); } public void delete(T item, @NonNull Runnable onDone) { - AsyncTask.execute(() -> { + asyncRunner.execute(() -> { deleteSync(item); Misc.onMainThread(onDone); }); diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/ProfileDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/ProfileDAO.java index e61e1fb5..3c8fb4c3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/ProfileDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/ProfileDAO.java @@ -17,8 +17,6 @@ package net.ktnx.mobileledger.dao; -import android.os.AsyncTask; - import androidx.lifecycle.LiveData; import androidx.room.Dao; import androidx.room.Delete; @@ -44,7 +42,7 @@ public abstract class ProfileDAO extends BaseDAO { return insertSync(item); } public void insertLast(Profile item, OnInsertedReceiver onInsertedReceiver) { - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { long id = insertLastSync(item); if (onInsertedReceiver != null) onInsertedReceiver.onInsert(id); @@ -90,7 +88,7 @@ public abstract class ProfileDAO extends BaseDAO { } } public void updateOrder(List list, Runnable onDone) { - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { updateOrderSync(list); if (onDone != null) onDone.run(); diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java index 6188f70e..ed2473b2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java @@ -17,8 +17,6 @@ package net.ktnx.mobileledger.dao; -import android.os.AsyncTask; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.lifecycle.LiveData; @@ -44,7 +42,7 @@ public abstract class TemplateHeaderDAO { public abstract long insertSync(TemplateHeader item); public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) { - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { insertSync(item); if (callback != null) Misc.onMainThread(callback); @@ -58,7 +56,7 @@ public abstract class TemplateHeaderDAO { public abstract void deleteSync(TemplateHeader item); public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) { - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { deleteSync(item); Misc.onMainThread(callback); }); @@ -130,7 +128,7 @@ public abstract class TemplateHeaderDAO { }); } public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) { - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { insertSync(item); if (callback != null) Misc.onMainThread(callback); @@ -138,7 +136,7 @@ public abstract class TemplateHeaderDAO { } public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable AsyncResultCallback callback) { - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { TemplateWithAccounts src = getTemplateWithAccountsSync(id); TemplateWithAccounts dup = src.createDuplicate(); dup.header.setName(dup.header.getName()); diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java index d7e8e16f..4032602c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java @@ -17,7 +17,6 @@ package net.ktnx.mobileledger.dao; -import android.os.AsyncTask; import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; @@ -217,7 +216,7 @@ public abstract class TransactionDAO extends BaseDAO { } } public void storeLast(TransactionWithAccounts rec) { - AsyncTask.execute(() -> appendSync(rec)); + BaseDAO.runAsync(() -> appendSync(rec)); } @androidx.room.Transaction public void appendSync(TransactionWithAccounts rec) { diff --git a/app/src/main/java/net/ktnx/mobileledger/db/Profile.java b/app/src/main/java/net/ktnx/mobileledger/db/Profile.java index a3ed1806..35230e2f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/Profile.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/Profile.java @@ -17,8 +17,6 @@ package net.ktnx.mobileledger.db; -import android.os.AsyncTask; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.room.ColumnInfo; @@ -28,6 +26,7 @@ import androidx.room.PrimaryKey; import androidx.room.Transaction; import net.ktnx.mobileledger.dao.AccountDAO; +import net.ktnx.mobileledger.dao.BaseDAO; import net.ktnx.mobileledger.dao.OptionDAO; import net.ktnx.mobileledger.dao.TransactionDAO; import net.ktnx.mobileledger.utils.Misc; @@ -242,7 +241,7 @@ public class Profile { trnDao.deleteSync(trnDao.getAllForProfileUnorderedSync(id)); } public void wipeAllData() { - AsyncTask.execute(this::wipeAllDataSync); + BaseDAO.runAsync(this::wipeAllDataSync); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java index 2887058f..d7da335d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java @@ -18,7 +18,6 @@ package net.ktnx.mobileledger.ui.account_summary; import android.content.res.Resources; -import android.os.AsyncTask; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -33,6 +32,7 @@ import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.dao.BaseDAO; import net.ktnx.mobileledger.databinding.AccountListRowBinding; import net.ktnx.mobileledger.databinding.AccountListSummaryRowBinding; import net.ktnx.mobileledger.db.Account; @@ -243,7 +243,7 @@ public class AccountSummaryAdapter extends RecyclerView.Adapter { + BaseDAO.runAsync(() -> { Account dbo = account.toDBO(); dbo.setExpanded(!dbo.isExpanded()); Logger.debug("accounts", @@ -275,7 +275,7 @@ public class AccountSummaryAdapter extends RecyclerView.Adapter { + BaseDAO.runAsync(() -> { Account dbo = account.toDBO(); DB.get() .getAccountDAO() diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java index 0b4a1fe0..1ffcbf8b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java @@ -18,12 +18,12 @@ package net.ktnx.mobileledger.ui.activity; import android.annotation.SuppressLint; -import android.os.AsyncTask; import android.os.Bundle; import androidx.annotation.Nullable; import net.ktnx.mobileledger.App; +import net.ktnx.mobileledger.dao.BaseDAO; import net.ktnx.mobileledger.dao.ProfileDAO; import net.ktnx.mobileledger.db.DB; import net.ktnx.mobileledger.db.Profile; @@ -103,7 +103,7 @@ public class ProfileThemedActivity extends CrashReportingActivity { initProfile(profileId); } protected void initProfile(long profileId) { - AsyncTask.execute(() -> initProfileAsync(profileId)); + BaseDAO.runAsync(() -> initProfileSync(profileId)); } private void initProfileAsync(long profileId) { ProfileDAO dao = DB.get() diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java index 50b8c63e..d4355a9b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java @@ -20,7 +20,6 @@ package net.ktnx.mobileledger.ui.new_transaction; import android.content.Context; import android.content.Intent; import android.database.AbstractCursor; -import android.os.AsyncTask; import android.os.Bundle; import android.os.ParcelFormatException; import android.util.TypedValue; @@ -43,6 +42,7 @@ import net.ktnx.mobileledger.async.DescriptionSelectedCallback; import net.ktnx.mobileledger.async.GeneralBackgroundTasks; import net.ktnx.mobileledger.async.SendTransactionTask; import net.ktnx.mobileledger.async.TaskCallback; +import net.ktnx.mobileledger.dao.BaseDAO; import net.ktnx.mobileledger.dao.TransactionDAO; import net.ktnx.mobileledger.databinding.ActivityNewTransactionBinding; import net.ktnx.mobileledger.db.DB; @@ -217,7 +217,7 @@ public class NewTransactionActivity extends ProfileThemedActivity else { navController.navigate(R.id.action_newTransactionSavingFragment_Success, b); - AsyncTask.execute(() -> commitToDb((LedgerTransaction) arg)); + BaseDAO.runAsync(() -> commitToDb((LedgerTransaction) arg)); } } public void commitToDb(LedgerTransaction tr) { @@ -366,7 +366,7 @@ public class NewTransactionActivity extends ProfileThemedActivity if (!model.accountListIsEmpty()) return; - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { String accFilter = mProfile.getPreferredAccountsFilter(); TransactionDAO trDao = DB.get() diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 547981e1..fce5967f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -20,7 +20,6 @@ package net.ktnx.mobileledger.ui.profiles; import android.app.Activity; import android.app.AlertDialog; import android.graphics.Typeface; -import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -46,6 +45,7 @@ import com.google.android.material.textfield.TextInputLayout; import net.ktnx.mobileledger.BuildConfig; import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.dao.BaseDAO; import net.ktnx.mobileledger.dao.ProfileDAO; import net.ktnx.mobileledger.databinding.ProfileDetailBinding; import net.ktnx.mobileledger.db.DB; @@ -126,10 +126,11 @@ public class ProfileDetailFragment extends Fragment { dao.getById(profileId) .observe(getViewLifecycleOwner(), profile -> { if (profile != null) - AsyncTask.execute(() -> { - dao.deleteSync(profile); - dao.updateOrderSync(dao.getAllOrderedSync()); - }); + BaseDAO.runAsync(() -> DB.get() + .runInTransaction(() -> { + dao.deleteSync(profile); + dao.updateOrderSync(dao.getAllOrderedSync()); + })); }); final FragmentActivity activity = getActivity(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java index 9a4682d9..414340cc 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java @@ -17,14 +17,13 @@ package net.ktnx.mobileledger.ui.templates; -import android.os.AsyncTask; - import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; import net.ktnx.mobileledger.BuildConfig; +import net.ktnx.mobileledger.dao.BaseDAO; import net.ktnx.mobileledger.dao.TemplateAccountDAO; import net.ktnx.mobileledger.dao.TemplateHeaderDAO; import net.ktnx.mobileledger.db.DB; @@ -208,7 +207,7 @@ public class TemplateDetailsViewModel extends ViewModel { Logger.debug("flow", "PatternDetailsViewModel.onSavePattern(); model=" + this); final List list = Objects.requireNonNull(items.getValue()); - AsyncTask.execute(() -> { + BaseDAO.runAsync(() -> { boolean newPattern = mPatternId == null || mPatternId <= 0; TemplateDetailsItem.Header modelHeader = list.get(0)