具有一个函数,该函数首先从数据库中获取数据,如果数据良好,则无需进一步提取即可返回。
将其更改为suspend
并添加coroutineScope
之后,它开始出现return is not allowed here
的编译器错误
如何从complete
内部获得收益或coroutineScope
?
override suspend fun doFetch() {
coroutineScope {
// is data from database is good enough then need to fetch from remote
val isDataFresh = loadDataFromDatabase()
if (isDataFresh) return . //<=== the comipler error "return is not allowed here"
}
// otherwise fetching from remote
// ... ... ...
}
}