Skip to content

Commit ad8778b

Browse files
authored
call arcore.destroy off of main thread Fixes #407 (#408)
* call arcore.destroy off of main thread * rename method as per PR comment --------- Co-authored-by: Sameer Jiwani <>
1 parent 2230c5a commit ad8778b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

arsceneview/src/main/java/io/github/sceneview/ar/ARSceneView.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.activity.ComponentActivity
77
import androidx.lifecycle.DefaultLifecycleObserver
88
import androidx.lifecycle.Lifecycle
99
import androidx.lifecycle.LifecycleOwner
10+
import androidx.lifecycle.coroutineScope
1011
import com.google.android.filament.Engine
1112
import com.google.android.filament.IndirectLight
1213
import com.google.android.filament.MaterialInstance
@@ -44,6 +45,9 @@ import io.github.sceneview.model.Model
4445
import io.github.sceneview.model.ModelInstance
4546
import io.github.sceneview.node.LightNode
4647
import io.github.sceneview.node.Node
48+
import kotlinx.coroutines.CoroutineScope
49+
import kotlinx.coroutines.Dispatchers
50+
import kotlinx.coroutines.launch
4751

4852
/**
4953
* A SurfaceView that integrates with ARCore and renders a scene
@@ -561,7 +565,7 @@ open class ARSceneView @JvmOverloads constructor(
561565

562566
override fun destroy() {
563567
if (!isDestroyed) {
564-
arCore.destroy()
568+
destroyARCore()
565569

566570
defaultCameraNode?.destroy()
567571
defaultCameraStream?.destroy()
@@ -573,6 +577,14 @@ open class ARSceneView @JvmOverloads constructor(
573577
super.destroy()
574578
}
575579

580+
fun destroyARCore() {
581+
val scope = lifecycle?.coroutineScope ?: CoroutineScope(Dispatchers.IO)
582+
scope.launch(Dispatchers.IO) {
583+
// destroy should be called off the main thread since it hangs for many seconds
584+
arCore.destroy()
585+
}
586+
}
587+
576588
class DefaultARCameraNode(engine: Engine) : ARCameraNode(engine) {
577589
init {
578590
// Set the exposure on the camera, this exposure follows the sunny f/16 rule
@@ -596,7 +608,7 @@ open class ARSceneView @JvmOverloads constructor(
596608
}
597609

598610
override fun onDestroy(owner: LifecycleOwner) {
599-
arCore.destroy()
611+
destroyARCore()
600612
}
601613
}
602614

0 commit comments

Comments
 (0)