'How can i return a list in GlobalScope (with sth like remember{}) in Jetpack Compose
I can return a String in GlobalScope by "remember { mutableStateOf("") }" and i try to return a list by sth like "remember { mutableStateListOf() }" and log.d to print it then the app will crush.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var n = 1
setContent {
WannaJsoupTheme {
var returnStr = remember { mutableStateOf("") }
var returnList = remember { mutableStateListOf<String>() }
mutableStateListOf<String>()
GlobalScope.launch(Dispatchers.IO) {
if (n == 1) {
val doc = Jsoup.connect(someUrl).get()
val box = doc.select("span[class=img]")
box.forEach {
returnList.add(it.select("img").attr("src"))
}
n++
}
// now i only can use returnStr.value = returnList.joinToString ("#")
// and split it later...
}
Log.d("TAG", returnList[0])
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
