回答編集履歴
1
説明の追加
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
|
}
|