同时进行火灾查询等待另一个完成

时间:2018-04-12 19:59:30

标签: reactjs react-native redux google-cloud-firestore react-native-firebase

在我的react-native应用程序中,我正在查询一个非常少量数据的集合,这些数据本身通常需要<1秒才能完成。我还想同时查询单独的集合以获取更大量的数据(~1MB),这些数据通常在5-10秒内完成。

我的问题是,如果我在较小的查询完成之前查询大型集合,则较小的查询将在较大的查询完成之前完成。

我的较小(价格)查询:

export const getPrices = () => {
  return dispatch => {
    dispatch({ type: GETTING_PRICES });
    let prices;
    firestore
      .collection('settings-daily-market')
      .orderBy('MarketDate', 'desc')
      .limit(1)
      .get()
      .then(snapshot => {
        snapshot.forEach(doc => {
          const priceUpdated = moment(doc.data().MarketDate).format(
            'MMMM D, YYYY'
          );
          const Pd = doc.data().PdMarket;
          const Pt = doc.data().PtMarket;
          const Rh = doc.data().RhMarket;
          const PdHedge = doc.data().PdHedge;
          const PtHedge = doc.data().PtHedge;
          const RhHedge = doc.data().RhHedge;
          prices = { Pd, Pt, Rh, PdHedge, PtHedge, RhHedge, priceUpdated };
        });
        dispatch({ type: FETCH_PRICES, payload: prices });
      });
  };
};

我更大的(目录)查询:

export const getCatalog = () => {
  return dispatch => {
    dispatch({ type: GETTING_CATALOG });
    let completeCatalog = {};
    firestore
      .collection('records-catalytic-converters')
      .get()
      .then(snapshot => {
        snapshot.forEach(
          doc => (completeCatalog[doc.data().Name] = doc.data())
        );
        dispatch({ type: GET_CATALOG, payload: completeCatalog });
      });
  };
};

基本上,FETCH_PRICES和GET_CATALOG调度同时发生,尽管其中一个(理论上)很久就完成了。

我有预感,这可能与我遇到的另一个问题有关。当Catalog查询中的.forEach()循环运行时,我的应用程序没有响应,并且在Catalog查询完成后,任何用户输入都会被注册。

有没有办法让forEach循环异步?

0 个答案:

没有答案
相关问题