-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix (note editor): App crashing after performing multiple clicks on add button #20131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
david-allison
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one nerd-sniped me, I tried something like this a couple of years ago, but wasn't happy with it.
Here's a thought I had now. Untested, but I feel it implements your logic nicely
Subject: [PATCH] A
---
Index: AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt (revision fa83782d721bf044805f1c9f2297a28cde3a7757)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt (date 1768737838195)
@@ -153,6 +153,7 @@
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.ui.setupNoteTypeSpinner
+import com.ichi2.anki.utils.RunOnlyOnce
import com.ichi2.anki.utils.ext.sharedPrefs
import com.ichi2.anki.utils.ext.showDialogFragment
import com.ichi2.anki.utils.ext.window
@@ -224,7 +225,7 @@
private var changed = false
private var isTagsEdited = false
private var isFieldEdited = false
-
+ private var addNoteJob = RunOnlyOnce(scope = lifecycleScope)
private var multimediaActionJob: Job? = null
private val getColUnsafe: Collection
@@ -1338,7 +1339,7 @@
reloadRequired = true
- lifecycleScope.launch {
+ addNoteJob.launch {
val noteFieldsCheck = checkNoteFieldsResponse(editorNote!!)
if (noteFieldsCheck is NoteFieldsCheckResult.Failure) {
addNoteErrorMessage = noteFieldsCheck.localizedMessage ?: getString(R.string.something_wrong)
Index: AnkiDroid/src/main/java/com/ichi2/anki/utils/OnlyOnce.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/utils/OnlyOnce.kt b/AnkiDroid/src/main/java/com/ichi2/anki/utils/OnlyOnce.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/utils/OnlyOnce.kt (revision fa83782d721bf044805f1c9f2297a28cde3a7757)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/utils/OnlyOnce.kt (date 1768737821418)
@@ -16,7 +16,9 @@
package com.ichi2.anki.utils
+import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
+import kotlinx.coroutines.launch
import timber.log.Timber
/**
@@ -51,3 +53,15 @@
}
}
}
+
+class RunOnlyOnce(private val scope: CoroutineScope) {
+ private var job: Job? = null
+
+ fun launch(block: suspend CoroutineScope.() -> Unit) {
+ if (job?.isActive == true) {
+ Timber.d("skipped multiple executions of job")
+ return
+ }
+ job = scope.launch(block = block)
+ }
+}
\ No newline at end of file631c239 to
486cdec
Compare
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, assuming it's tested
Optional:
- add
Fixes 17375to the commit message description (without the#) to help anyone looking through the history - Add a co-author:
Co-authored-by: David Allison <62114487+david-allison@users.noreply.github.com>via a trailer at the end of the commit description if you feel a contribution was meaningful enough to warrant a pip on their GitHub heatmap for the change- This is a git trailer: https://git-scm.com/docs/git-interpret-trailers
… editor Fixes 17375 Co-authored-by: David Allison <62114487+david-allison@users.noreply.github.com>
486cdec to
ef9dbc4
Compare
Purpose / Description
This bug happens when you tap add too many times in a small window, it ends up crashing ankidroid
Fixes
BackendDbFileTooOldException: calledResult::unwrap()on anErrvalue: PoisonError { .. } #17375Approach
Tried different approaches like using a boolean variable and mutex before realising that the issue was with the lifecycle in savenote()
How Has This Been Tested?
I used an auto clicker to simulate a lot of clicks a second (1 ms interval) and the app would just crash immediately, after applying the change however it just takes the first click and performs the add
Checklist
Please, go through these checks before submitting the PR.