我已经从Google控制台生成了用于Places API密钥的服务器密钥.但每当我点击服务时,我都会收到"提供的API密钥已过期"错误.
我已经尝试重新生成密钥但仍然出现相同的错误.
我可以在 kotlin flow 中使用 async{} 吗?
场景:API 调用后,我得到了需要解析的 200 个对象的列表(转换为 UIObject)。我正在尝试并行处理这个列表。下面是伪代码:
fun getUIObjectListFlow():Flow<List<UIObject>> {
flow<List<UIObject>> {
while (stream.hasNext()) {
val data = stream.getData() // reading from an input stream. Data comes in chunk
val firstHalfDeffered = async(Dispatchers.IO) { /* process first half of the list that data obj contains*/ }
val secondHalfDeffered = async(Dispatchers.IO) { /*process second half of the list that data obj contains */ }
val processedList = firstHalfDeffered.await() + secondHalfDeffered.await() // just pseudo code
emit(processedList)
} …Run Code Online (Sandbox Code Playgroud) 挂起功能在单独的线程上运行?如果不是,那么性能优势是什么?
suspend fun requestToken():Token {..} // takes 2 sec to complete
suspend fun createPost (token:Token){..} // takes 3 sec to complete
suspend fun postItem() {
val token = requestToken()
val post =createPost(token)
processPost(post)
}
Run Code Online (Sandbox Code Playgroud)
因此,当我们到达processPost(post)并且如果挂起函数没有在单独的线程上运行,那么我们必须等待requestToken()和createPost(token)方法完成(即 2+3=5 秒)。根据作者的说法,暂停是异步的,但是如果我们没有产生任何新线程,那么我们如何实现异步行为?