经过阅读后,我了解了Cloud Firestore的isFromCache()来检查数据是否来自缓存。但是,我不知道如何使用它,并且文档中也没有太多关于如何使用它的细节。
我使用FirestoreRecyclerView,以下是我的onViewCreated():
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.firestoreRecyclerView
val db = Firebase.firestore
val query : Query = db.collectionGroup("fire")
val options : FirestoreRecyclerOptions<FireModel> =
FirestoreRecyclerOptions.Builder<FireModel>()
.setQuery(query, FireModel::class.java)
.build()
adapter = object : FirestoreRecyclerAdapter<FireModel, FireViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FireViewHolder{
val v : View = LayoutInflater.from(parent.context).inflate(R.layout.fire_cardview, parent, false)
return FireViewHolder(v)
}
override fun onBindViewHolder(
holder: FireViewHolder,
position: Int,
model: FireModel
) {
holder.name.text = model.name.name
holder.address.text = model.address.unitName
holder.age.text = model.age.toString()
}
class FireViewHolder(v : View) : RecyclerView.ViewHolder (v) {
var name : TextView = v.nameTextView
var address: TextView = v.addressTextView
var age: TextView = v.ageValueTextView
}
和我的模型班:
@Keep
@Parcelize
class FireModel (
var name : String = "",
var address : String = "",
var age : Int = 0
) : Parcelable