Skip to content

Commit 52c6b48

Browse files
Refactor: Move survey navigation DB query to background thread
Moves the blocking Realm query in `DashboardActivity.handleSurveyNavigation` to a background thread to prevent potential ANRs. - Converted `handleSurveyNavigation` to a `suspend` function. - Wrapped the Realm query in `withContext(Dispatchers.IO)`. - Used `realm.copyFromRealm` to pass the result to the main thread safely. - Updated the call site in `handleNotificationIntent` to launch the function in `lifecycleScope`.
1 parent 83a17ba commit 52c6b48

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

app/src/main/java/org/ole/planet/myplanet/ui/dashboard/DashboardActivity.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ class DashboardActivity : DashboardElementActivity(), OnHomeItemClickListener, N
387387

388388
when (notificationType) {
389389
NotificationUtils.TYPE_SURVEY -> {
390-
handleSurveyNavigation(relatedId)
390+
lifecycleScope.launch {
391+
handleSurveyNavigation(relatedId)
392+
}
391393
}
392394
NotificationUtils.TYPE_TASK -> {
393395
lifecycleScope.launch {
@@ -411,10 +413,16 @@ class DashboardActivity : DashboardElementActivity(), OnHomeItemClickListener, N
411413
}
412414
}
413415

414-
private fun handleSurveyNavigation(surveyId: String?) {
416+
private suspend fun handleSurveyNavigation(surveyId: String?) {
415417
if (surveyId != null) {
416-
val currentStepExam = mRealm.where(RealmStepExam::class.java).equalTo("name", surveyId)
417-
.findFirst()
418+
val currentStepExam = withContext(Dispatchers.IO) {
419+
Realm.getDefaultInstance().use { realm ->
420+
realm.where(RealmStepExam::class.java).equalTo("name", surveyId)
421+
.findFirst()?.let {
422+
realm.copyFromRealm(it)
423+
}
424+
}
425+
}
418426
AdapterMySubmission.openSurvey(this, currentStepExam?.id, false, false, "")
419427
}
420428
}

0 commit comments

Comments
 (0)