合并来自不同可观察量的数据,并根据数据可用性选择不同的获取策略



我有一个情况,在这种情况下,通过获取Item对象,应该发生以下三种情况之一:

  • 如果缓存中不存在该项(又名 null),则从 api1api2 加载数据,合并数据并返回Observable
  • 如果项目存在于缓存中,但缺少其中的某个部分,请从api2加载数据,并将其与缓存中已有的数据合并
  • 如果整个数据在缓存中可用,只需将其返回即可。

到目前为止,这是我设法想出的最好的:

val cacheObservable = cache.fetchItem(itemId);
val api1Observable = api1.fetchItem(itemId);
val api2Observable = api2.fetchItem(itemId);
val resultObservable = cacheObservable!!.flatMap { item: Item? ->
    if (item == null) {
        /* Get the item data, and the full text separately, then combine them */
        Observable.zip(api1Observable, api2Observable, { itemData, itemText ->
            itemData.apply { text = itemText }
        });
    } else if (item.text.isNullOrEmpty()) {
        /* Get the full text only, then add it to the already cached version */
        cacheObservable.zipWith(api2Observable, { cachedItem, itemText -> cachedItem.apply { text = itemText; } });
    } else {
        /* if the data and the full text are provided, simply return */
        Observable.just(item);
    }
}.doOnNext { item -> cache.saveOrUpdateItem(item); }
return resultObservable;

到目前为止,这工作正常,但我一直想知道,是否有一种更具声明性的方式来达到相同的效果。欢迎提出建议。

我不知道 Kotlin 语法,但这里有一个你可以翻译的 Java 示例。它需要cache.fetchItem(itemId)才能完成,而不是在项目不在缓存中时发出null

如果cacheFetch发出一个项目,flatMap()将确保该项目具有文本,并在需要时从网络中获取它。

如果缓存中没有任何内容,fullFetch将从网络中获取它。

concatWith(fullFetch).take(1)将确保仅在缓存中没有任何内容时才订阅fullFetch

仅当项目已更新时,该项目才会保存到缓存中。

Observable<Item> cacheFetch = cache.fetchItem(itemId);
Observable<Item> fullFetch =
  Observable
    .zip(
      api1.fetchItem(itemId),
      api2.fetchItem(itemId),
      (item, itemText) -> {
          item.text = itemText;
          return item;
      })
    .doOnNext(cache::saveOrUpdateItem);
Observable<Item> resultObservable =
  cacheFetch
    .flatMap(item -> {
      if (item.text != null && !item.text.isEmpty()) {
        return Observable.just(item);
      }
      return api2
        .fetchItem(itemId)
        .map(itemText -> {
          item.text = itemText;
          return item;
        })
        .doOnNext(cache::saveOrUpdateItem);
    })
    .concatWith(fullFetch)
    .take(1);
  }

最新更新