从coroutine内部返回时,kotlin协程会出错

时间:2019-07-01 20:22:34

标签: kotlin-coroutines coroutinescope

具有一个函数,该函数首先从数据库中获取数据,如果数据良好,则无需进一步提取即可返回。

将其更改为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
        // ... ... ...
    }
}

0 个答案:

没有答案