首页 > 解决方案 > 如何同步这个包含函数的 kotlin 函数?

问题描述

例子:

fun getListOfPlaces() : List<String> {
    Log.d("TAG", "Before attaching the listener!");
    val places = ArrayList<String>()
    placesRef.get().addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d("TAG", "Inside onComplete function!");
            for (document in task.result) {
                val name = document.data["name"].toString()
                places.add(name)
            }
        }
    }
    Log.d("TAG", "After attaching the listener!");
    return list;
}

而logcat将是

Before attaching the listener!

After attaching the listener!

Inside onComplete function!

我想让这个函数与之前 -> 内部 -> 之后一样同步。

我该怎么做?

标签: kotlinsynchronizationkotlin-coroutines

解决方案


推荐阅读