FirebaseFlowExample Firebase Firestore与Kotlin Coroutines Flow结合示例

Firebase 的 Firestore 加上 Kotlin Coroutines 的 Flow,组合起来其实挺香的,尤其是在异步数据流这块。这个开源项目就是个不错的例子。它展示了怎么在 Android 里用 Flow 监听 Firestore 的实时数据,代码清晰,逻辑也好懂。你不需要去研究一堆回调,Flow 配合addSnapshotListener,数据一变 UI 马上更新,响应也快。

Flow 的好处你早就听说过:声明式写法、支持背压、可以链式操作(.map().filter()这些都能用上)。这个项目还演示了怎么在 Activity 或 Fragment 生命周期里优雅地用 Flow 收集数据,不用担心内存泄漏。

FireStore 本身的特性也适合搞实时 UI,比如聊天、评论、动态列表这些。它的文档结构也灵活,用起来就像操作一个 JSON 对象。再加上离线缓存支持,就算没网也能读写,挺适合做一些对用户体验要求高的 App。

项目里还有一个小亮点,就是如何把 Firestore 的文档快照转成你 App 里用的数据模型,比如转成User或者Post这种自定义类,Flow 中直接,代码也清爽。

如果你打算用 Kotlin 写 Android,又跟 Firebase 打交道,推荐你撸一遍这个项目。熟悉之后你会发现,异步数据没那么难,写起来还挺优雅。

zip
FirebaseFlowExample-master.zip 预估大小:40个文件
folder
FirebaseFlowExample-master 文件夹
file
gradlew 5KB
file
settings.gradle 56B
folder
gradle 文件夹
folder
wrapper 文件夹
file
gradle-wrapper.properties 232B
file
gradle-wrapper.jar 53KB
file
README.md 286B
file
gradlew.bat 2KB
file
gradle.properties 1KB
file
.gitignore 93B
folder
app 文件夹
folder
src 文件夹
folder
androidTest 文件夹
folder
java 文件夹
folder
dev 文件夹
folder
shreyaspatil 文件夹
folder
firebase 文件夹
folder
coroutines 文件夹
file
ExampleInstrumentedTest.kt 709B
folder
test 文件夹
folder
java 文件夹
folder
dev 文件夹
folder
shreyaspatil 文件夹
folder
firebase 文件夹
folder
coroutines 文件夹
file
ExampleUnitTest.kt 371B
folder
main 文件夹
file
AndroidManifest.xml 738B
folder
res 文件夹
folder
mipmap-hdpi 文件夹
file
ic_launcher.png 4KB
file
ic_launcher_round.png 5KB
folder
mipmap-xxxhdpi 文件夹
file
ic_launcher.png 10KB
file
ic_launcher_round.png 16KB
folder
mipmap-xhdpi 文件夹
file
ic_launcher.png 5KB
file
ic_launcher_round.png 7KB
folder
mipmap-anydpi-v26 文件夹
file
ic_launcher.xml 272B
file
ic_launcher_round.xml 272B
folder
mipmap-mdpi 文件夹
file
ic_launcher.png 3KB
file
ic_launcher_round.png 3KB
folder
drawable 文件夹
file
ic_launcher_background.xml 5KB
folder
layout 文件夹
file
activity_main.xml 2KB
folder
drawable-v24 文件夹
file
ic_launcher_foreground.xml 2KB
folder
values 文件夹
file
colors.xml 208B
file
strings.xml 241B
file
styles.xml 383B
folder
mipmap-xxhdpi 文件夹
file
ic_launcher.png 8KB
file
ic_launcher_round.png 12KB
folder
java 文件夹
folder
dev 文件夹
folder
shreyaspatil 文件夹
folder
firebase 文件夹
folder
coroutines 文件夹
folder
ui 文件夹
folder
main 文件夹
file
MainViewModel.kt 480B
file
MainActivity.kt 3KB
file
MainViewModelFactory.kt 532B
folder
model 文件夹
file
Post.kt 143B
file
State.kt 397B
file
Constants.kt 106B
folder
repository 文件夹
file
PostsRepository.kt 2KB
file
.gitignore 29B
file
build.gradle 2KB
file
proguard-rules.pro 751B
file
build.gradle 719B
...
zip 文件大小:147.59KB