teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

説明の追加

2020/03/01 23:33

投稿

kakajika
kakajika

スコア3133

answer CHANGED
@@ -1,11 +1,11 @@
1
1
  コメントでsuspendとあったので、coroutinesを使った実装例です。
2
- [org.jetbrains.kotlinx:kotlinx-coroutines-play-services](https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-play-services) を利用しています。
2
+ [org.jetbrains.kotlinx:kotlinx-coroutines-play-services](https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-play-services) のawaitを利用しています。
3
3
 
4
4
  ```kotlin
5
5
  suspend fun getUser(userId: String): User? {
6
6
  return userRef.document(userId)
7
7
  .get()
8
- .await()
8
+ .await() // データが取得されるまで待機
9
9
  .takeIf { it.exists() } // existsがfalseならnullを返す
10
10
  ?.toObject(User::class.java)
11
11
  }