Skip to content

Commit

Permalink
fix: ANR caused by operationRepo.enqueue while loading is not complete
Browse files Browse the repository at this point in the history
Making the enqueue logic to be done in the background
  • Loading branch information
jinliu9508 committed Dec 16, 2024
1 parent fc48c72 commit 0de1885
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.onesignal.common.threading

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext

object OSPrimaryCoroutineScope {
// CoroutineScope tied to the main thread
private val mainScope = CoroutineScope(newSingleThreadContext(name = "OSPrimaryCoroutineScope"))

/**
* Executes the given [block] on the OS primary coroutine scope.
*/
fun execute(block: suspend () -> Unit) {
mainScope.launch {
block()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onesignal.session.internal.session.impl

import com.onesignal.common.threading.OSPrimaryCoroutineScope
import com.onesignal.common.threading.suspendifyOnThread
import com.onesignal.core.internal.config.ConfigModelStore
import com.onesignal.core.internal.operations.IOperationRepo
Expand Down Expand Up @@ -40,7 +41,10 @@ internal class SessionListener(
}

override fun onSessionStarted() {
_operationRepo.enqueue(TrackSessionStartOperation(_configModelStore.model.appId, _identityModelStore.model.onesignalId), true)
// enqueue the operation in background
OSPrimaryCoroutineScope.execute {
_operationRepo.enqueue(TrackSessionStartOperation(_configModelStore.model.appId, _identityModelStore.model.onesignalId), true)
}
}

override fun onSessionActive() {
Expand All @@ -54,9 +58,12 @@ internal class SessionListener(
Logging.error("SessionListener.onSessionEnded sending duration of $durationInSeconds seconds")
}

_operationRepo.enqueue(
TrackSessionEndOperation(_configModelStore.model.appId, _identityModelStore.model.onesignalId, durationInSeconds),
)
// enqueue the operation in background
OSPrimaryCoroutineScope.execute {
_operationRepo.enqueue(
TrackSessionEndOperation(_configModelStore.model.appId, _identityModelStore.model.onesignalId, durationInSeconds),
)
}

suspendifyOnThread {
_outcomeEventsController.sendSessionEndOutcomeEvent(durationInSeconds)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.onesignal.user.internal.service

import com.onesignal.common.IDManager
import com.onesignal.common.threading.OSPrimaryCoroutineScope
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.core.internal.config.ConfigModelStore
import com.onesignal.core.internal.operations.IOperationRepo
Expand Down Expand Up @@ -28,12 +29,14 @@ class UserRefreshService(
return
}

_operationRepo.enqueue(
RefreshUserOperation(
_configModelStore.model.appId,
_identityModelStore.model.onesignalId,
),
)
OSPrimaryCoroutineScope.execute {
_operationRepo.enqueue(
RefreshUserOperation(
_configModelStore.model.appId,
_identityModelStore.model.onesignalId,
),
)
}
}

override fun start() = _sessionService.subscribe(this)
Expand Down

0 comments on commit 0de1885

Please sign in to comment.