Delay heavy tasks to prevent ServiceDidNotStartInTimeException (for example)

This commit is contained in:
Sylvain Berfini 2025-03-25 11:17:57 +01:00
parent 7018cd3442
commit d150027c24
2 changed files with 20 additions and 4 deletions

View file

@ -107,9 +107,9 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
}
Log.i("$TAG Load finished, found ${cursor.count} entries in cursor")
coreContext.postOnCoreThread {
coreContext.postOnCoreThreadWhenAvailableForHeavyTask({
parseFriends(cursor)
}
}, "parse friends")
}
@MainThread
@ -265,9 +265,9 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
Log.i("$TAG Contacts parsed, posting another task to handle adding them (or not)")
// Re-post another task to allow other tasks on Core thread
coreContext.postOnCoreThread {
coreContext.postOnCoreThreadWhenAvailableForHeavyTask({
addFriendsIfNeeded()
}
}, "add friends to Core")
} catch (sde: StaleDataException) {
Log.e("$TAG State Data Exception: $sde")
} catch (ise: IllegalStateException) {

View file

@ -38,6 +38,7 @@ import androidx.lifecycle.MutableLiveData
import com.google.firebase.crashlytics.FirebaseCrashlytics
import kotlin.system.exitProcess
import org.linphone.BuildConfig
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.contacts.ContactsManager
import org.linphone.core.tools.Log
@ -653,6 +654,21 @@ class CoreContext
}
}
@AnyThread
fun postOnCoreThreadWhenAvailableForHeavyTask(@WorkerThread lambda: (core: Core) -> Unit, name: String) {
postOnCoreThread {
if (core.callsNb >= 1) {
Log.i("$TAG At least one call is active, wait until there is no more call before executing lambda [$name] (checking again in 1 sec)")
coreContext.postOnCoreThreadDelayed({
postOnCoreThreadWhenAvailableForHeavyTask(lambda, name)
}, 1000)
} else {
Log.i("$TAG No active call at the moment, executing lambda [$name] right now")
lambda.invoke(core)
}
}
}
@AnyThread
fun postOnMainThread(
@UiThread lambda: () -> Unit