william
03/07/2021, 8:58 PMinit
block and then top level value assignments causing an exception. heres a min example:
class Foo(bar: Bar) {
init {
CoroutineScope(Dispatchers.Default).launch {
bar.buzz()
}
}
val a = 3
}
in the init block since bar is accessed, it seems like that is causing the entire foo object to also get frozen
then when it gets to val a = 3
during runtime - it complains about foo instance already being frozen. how can i go about fixing this?mbonnin
03/07/2021, 9:01 PMa
before launching the coroutine ?russhwolf
03/07/2021, 9:02 PMbar
is just a constructor parameter and not a property of Foo
(eg val bar: Bar
) then I wouldn’t expect the whole Foo
to get frozen by that init block.a
firstmbonnin
03/07/2021, 9:03 PMFoo
to be frozen there.ensureNeverFrozen
, sometimes that helps debugging where something becomes frozenwilliam
03/07/2021, 9:11 PMinit
block like i am now hmm. maybe i'll need to add a start
function on my class or similarrusshwolf
03/07/2021, 9:14 PMclass Foo(val bar: Bar) {
init {
CoroutineScope(Dispatchers.Default).launch {
bar.buzz()
}
}
}
then you can refactor like this to avoid freezing Foo
class Foo(bar: Bar) {
val bar = bar // need to suppress a warning here
init {
CoroutineScope(Dispatchers.Default).launch {
bar.buzz()
}
}
}
bar
will still be frozen but there won’t be a this
reference inside the coroutine stuff to freeze the whole Foo
william
03/07/2021, 9:20 PM