【问题标题】:Transform retrieved livedata from different sources (storage, firestore) via Transformations通过转换转换从不同来源(存储、火库)检索到的实时数据
【发布时间】:2020-08-21 05:10:03
【问题描述】:

以下情况:

我正在通过 LiveData 从 Firestore 中检索数据,以将其显示在 RecyclerView 中,如下所示:

val placesDataSet: LiveData<ArrayList<Pair<String, PlaceModel>>> = db.getCollection("places")

PlaceModel 有一个属性previewImageUrl: String,它将被传递给RecyclerView.Adapter 内的Picasso 图像库。

在检索到“地点”信息(这是 Pair<documentId: String, place: PlaceModelArrayList 之后,我想在下一步中从 Firebase 存储中获取 previewImageUrl 并将其映射到相应的 PlaceModel 对象,然后再将其显示在RecyclerView

存储数据结构如下:images/$documentId/preview_image.jpg,因此我需要先获取位置信息(因为documentId)才能识别正确的图像文件夹。

如何使用Transformations 处理这个问题?我还有一个名为getDownloadUrl(path: String): MutableLiveData<Resource<String>> 的函数,它从存储中返回提供的path 的downloadUrl。

到目前为止我尝试过的是:

val dataSetFinalState = Transformations.map(placesDataSet) {
    mapDownloadUrl(it)
}

private fun mapDownloadUrl(arrayList: ArrayList<Pair<String, PlaceModel>>): 
 ArrayList<Pair<String, PlaceModel>> {
    arrayList.forEach {
        it.second.previewImageUrl = // How to call getDownloadUrl(path: String) here and wait for response?
    }
    return arrayList
}

【问题讨论】:

    标签: android-recyclerview google-cloud-firestore firebase-storage picasso android-livedata


    【解决方案1】:

    不幸的是,我无法通过转换解决这个问题,所以我决定选择另一种方法。上传预览图像后,我尝试获取下载 url 以将此信息存储到数据库中。

    这个方法的问题是我使用了firebase的图像调整扩展和删除原始图像选项。创建调整大小的图像需要一些时间,因此我在下一步中出错,因为图像不存在。但我用重试机制解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 2019-08-04
      相关资源
      最近更新 更多