Skip to content

Commit

Permalink
ANR: do not use main thread to check to update single show.
Browse files Browse the repository at this point in the history
This should fix the top reported ANR.
  • Loading branch information
UweTrottmann committed May 18, 2022
1 parent aa06b63 commit 5e3b9c1
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions app/src/main/java/com/battlelancer/seriesguide/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.battlelancer.seriesguide.ui

import android.os.Handler
import android.os.Looper
import android.text.format.DateUtils
import androidx.lifecycle.lifecycleScope
import com.battlelancer.seriesguide.dataliberation.BackupSettings
import com.battlelancer.seriesguide.shows.tools.AddShowTask.OnShowAddedEvent
import com.battlelancer.seriesguide.sync.SgSyncAdapter
import com.battlelancer.seriesguide.sync.SgSyncAdapter.Companion.requestSyncIfTime
import com.battlelancer.seriesguide.traktapi.TraktTask.TraktActionCompleteEvent
import com.battlelancer.seriesguide.shows.tools.AddShowTask.OnShowAddedEvent
import com.battlelancer.seriesguide.util.DBUtils.DatabaseErrorEvent
import com.battlelancer.seriesguide.util.TaskManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
Expand All @@ -22,9 +24,6 @@ import org.greenrobot.eventbus.ThreadMode
*/
abstract class BaseActivity : BaseThemeActivity() {

private var handler: Handler? = null
private var updateShowRunnable: Runnable? = null

override fun onStart() {
super.onStart()
// make sync interfering with backup task less likely
Expand All @@ -45,11 +44,6 @@ abstract class BaseActivity : BaseThemeActivity() {

override fun onStop() {
super.onStop()
val handler = handler
val updateShowRunnable = updateShowRunnable
if (handler != null && updateShowRunnable != null) {
handler.removeCallbacks(updateShowRunnable)
}
unregisterEventBus()
}

Expand Down Expand Up @@ -126,13 +120,11 @@ abstract class BaseActivity : BaseThemeActivity() {
* See [SgSyncAdapter.requestSyncIfTime].
*/
protected fun updateShowDelayed(showId: Long) {
val handler = handler ?: Handler(Looper.getMainLooper())
.also { handler = it }

// delay sync request to avoid slowing down UI
val context = applicationContext
val updateShowRunnable = Runnable { requestSyncIfTime(context, showId) }
.also { updateShowRunnable = it }
handler.postDelayed(updateShowRunnable, DateUtils.SECOND_IN_MILLIS)
lifecycleScope.launch(Dispatchers.IO) {
// Delay sync request to avoid slowing down UI.
delay(DateUtils.SECOND_IN_MILLIS)
requestSyncIfTime(context, showId)
}
}
}

0 comments on commit 5e3b9c1

Please sign in to comment.