Kotlin:如何从对象中等待函数一个从片段中启动另一个



我想让我的片段首先结束启动作业,然后完成其余的代码,但它没有,我不明白为什么。


runBlocking {
val job = launch {
Stopers.ITEMS.clear()
val database =
Firebase.database("myurl")
val user = Firebase.auth.currentUser
Log.e(TAG, "User: ${user?.uid.toString()}")
val myRef = database.getReference("${user?.uid.toString()}")
myRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
Stopers.ITEMS.clear()
Stopers.COUNT = dataSnapshot.child("Number_of_stopers").getValue<Long>()!!.toLong()
for (i in 1..Stopers.COUNT) {
.getValue<String>().toString()
Stopers.addStoper()
}
})
}
job.join()
adapter = MyStoperRecyclerViewAdapter(Stopers.ITEMS, eventListListener)
Log.d(TAG,"Adapter: done")
}

好吧,问题是你在runBlocking内部的调用作业,试试这种方法。

val job = launch { 
{
//your first code
}
}
runBlocking {
job.join()
//do what you wanted to do after
}

相关内容

最新更新