11

开发需要一次性向同一端点发送多个 API 调用的应用程序。

例如 - 目录浏览场景,需要通过对当前文件夹中的所有文件夹发送 get 调用来获取目录结构。问题是,正确改造中的所有文件夹的响应都是分开的,但是 LiveData observable 只为整个列表提供了一个响应。

目录结构:-

test -> temp -> temp1 -> temp2 
                      -> temp3
                      -> temp4

可观察到以侦听回调:-

mViewModel.getServerFilesLiveData().observe(this, browseServerDataResource -> {
      if (browseServerDataResource != null) {
        if (browseServerDataResource.status == APIClientStatus.Status.SUCCESS) {
          if (browseServerDataResource.data != null) {
            Timber.i("Got data for path %s in Observable", browseServerDataResource.data.path);
            if (browseServerDataResource.data.folderList != null
              && browseServerDataResource.data.folderList.size() > 0) {
              for (final String name : browseServerDataResource.data.folderList) {
                final ServerDirectoryPathInfo pathInfo = new ServerDirectoryPathInfo();
                pathInfo.completePath = browseServerDataResource.data.path + "/" + name;
                getFolderDownloadPath(pathInfo.completePath);
              }
            }
            mFolderCountToParse--;
            Timber.d("Folders left to parse %d", mFolderCountToParse);
            if (mFolderCountToParse == 0) {
              showToast("Parsed all folders");
            }
          }
        }
      }
    });

调用以获取数据的功能:-

  private void getFolderDownloadPath(@NonNull final String path) {
    mViewModel.getServerFiles(path);
    mFolderCountToParse++;
  }

改造对服务器的调用:-

  public LiveData<Resource<BrowseServerData>> getServerFiles(@NonNull final String additionalUrl) {
    final MutableLiveData<Resource<BrowseServerData>> data = new MutableLiveData<>();
    final String url = mMySharedPreferences.getCurrentUrl()
      + AppConstants.DIRECTORY_END_POINT
      + AppConstants.PATH_END_POINT
      + (TextUtils.isEmpty(additionalUrl) ? "" : additionalUrl);
    Timber.i("Requesting data for - api %s", url);
    mAPI.getServerFiles(url, mMySharedPreferences.getNetworkName())
      .enqueue(new Callback<BrowseServerData>() {
        @Override
        public void onResponse(
          @NonNull Call<BrowseServerData> call, @NonNull Response<BrowseServerData> response
        ) {
          if (response.body() != null && response.isSuccessful()) {
            if (!TextUtils.isEmpty(response.body().path)) {
              Timber.i("Got response for = %s in Retrofit", response.body().path);
            }
            data.setValue(
              new Resource<>(APIClientStatus.Status.SUCCESS, response.body(), null, null));
          } else {
            ErrorMessage errorMessage = null;
            try {
              errorMessage = Utility.getApiError(response, mRetrofit);
            } catch (IOException e) {
              e.printStackTrace();
            }
            if (errorMessage != null) {
              data.setValue(
                new Resource<>(APIClientStatus.Status.ERROR, null, errorMessage.message(), call));
            } else {
              data.setValue(
                new Resource<>(APIClientStatus.Status.ERROR, null, response.message(), call));
            }
          }
        }

        @Override
        public void onFailure(@NonNull Call<BrowseServerData> call, @NonNull Throwable throwable) {
          data.setValue(
            new Resource<>(APIClientStatus.Status.ERROR, null, throwable.getMessage(), throwable,
              call));
        }
      });
    return data;
  }

数据如下: -

I: Got response for = ./test in Retrofit
I: Got data for path ./test in Observable
I: Got response for = ./test/temp in Retrofit
I: Got data for path ./test/temp in Observable
I: Got response for = ./test/temp/temp1 in Retrofit
I: Got data for path ./test/temp/temp1 in Observable
I: Got response for = ./test/temp/temp1/temp2 in Retrofit
I: Got response for = ./test/temp/temp1/temp4 in Retrofit
I: Got response for = ./test/temp/temp1/temp3 in Retrofit
I: Got data for path ./test/temp/temp1/temp3 in Observable

如您所见,数据仅针对一个文件夹出现在 Observable 中temp3

当在拨打电话时添加随机延迟时,数据会正确出现:-

new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                    getFolderDownloadPath(pathInfo.completePath);
                  }
                }, new Random().nextInt(10000 - 1000) + 1000);

现在至少数据来自 3 个文件夹中的 2 个:-

I: Got response for = . in Retrofit
I: Got data for path . in Observable
I: Got data for the current directory, don't need it, skipping
I: Got response for = ./test in Retrofit
I: Got data for path ./test in Observable
I: Got response for = ./test/temp in Retrofit
I: Got data for path ./test/temp in Observable
I: Got response for = ./test/temp/temp1 in Retrofit
I: Got data for path ./test/temp/temp1 in Observable
I: Got response for = ./test/temp/temp1/temp3 in Retrofit
I: Got response for = ./test/temp/temp1/temp2 in Retrofit
I: Got data for path ./test/temp/temp1/temp2 in Observable
I: Got response for = ./test/temp/temp1/temp4 in Retrofit
I: Got data for path ./test/temp/temp1/temp4 in Observable

任何想法为什么会发生这种情况以及解决方法?

更新:- 添加有助于调用服务器的 ViewModel 构造函数

@Inject
  BrowseHubMediaViewModel(@NonNull Application application, @NonNull APIClient mAPIClient) {
    super(application);
    mGetServerFilesMutable = new MutableLiveData<>();
    mGetServerFilesLiveData =
      Transformations.switchMap(mGetServerFilesMutable, mAPIClient::getServerFiles);
}

从 ViewModel 获取 Observable

  /**
   * Observer to listen for file listing in server
   *
   * @return LiveData<Resource<BrowseServerData>>
   */
  public LiveData<Resource<BrowseServerData>> getServerFilesLiveData() {
    return mGetServerFilesLiveData;
  }
4

2 回答 2

3

switchmap 丢弃所有以前的项目,只取最新的项目。

I:在改造中
获得了 = ./test/temp/temp1/temp2 的响应 I:在改造中获得了 = ./test/temp/temp1/temp4 的响应 I:在改造中
获得了 = ./test/temp/temp1/temp3 的响应改造
I:在 Observable 中获取路径 ./test/temp/temp1/temp3 的数据

您已经按顺序调用了 temp2 temp4 和 temp3,而当 temp2 和 temp4 的数据出现时,您正在调用 temp3。所以 temp2 和 temp3 的 observable 将被删除,只返回 temp4 的 Observable。

我认为这可能会解决您的问题。您可以阅读有关 switchMap 的更多信息。那么它会更清楚。

于 2018-02-02T07:09:07.057 回答
2

@niketshah09 的提示提示我找到解决方案。正如@niketshah09 所描述的,问题是Transformations.switchMap()在多个回调快速到达时删除最后一个返回的调用。解决方案是使用MediatorLiveDatawhich 将合并所有调用并确保我们获得所有回调。例如 -

final LiveData<Resource<BrowseServerData>> newParsingFolderLiveData = mAPIClient.getServerFiles(completePath);
folderBrowsingMediator.addSource(newParsingFolderLiveData, folderBrowsingMediator::setValue);

接下来,我们必须观察 onMediatorLiveData而不是LiveData。虽然 的功能MediatorLiveData是确保我们过滤和使用正确的流,这取决于编码逻辑,但在这种情况下,我们想要获取所有回调,因此不对回调应用过滤。

这样我就得到了所有的回调,如果有人不明白,请告诉我。

于 2018-02-19T07:18:02.670 回答