【问题标题】:fetching data for the second time increments my long variable by 1, causing it not possible to reload data more than once第二次获取数据会使我的 long 变量增加 1,导致无法多次重新加载数据
【发布时间】:2019-09-04 12:29:07
【问题描述】:

我有一些相互调用的数据获取方法,以便根据之前获取的数据获取数据。

我的问题是我持有一个名为“allContestsCounterIndex”的长变量,该变量仅在 2 个特定位置递增,并且由于某种原因在第一次获取数据后,再次单击以尝试第二次获取它的值以 1 开头,这意味着我无法继续,因为它使逻辑卡住了。

这是我正在使用的功能 -


private void updateContestCallCounter(long childrenCount) {
    allContestsCounterIndex++;
    if (allContestsCounterIndex == (childrenCount - 1)) { // at this point at the second time this variable starts with a value of 1 instead of 0
      fetchWinnersFromOurValidContests();
    }
  }



private void fetchAllEndedStatusContests(DataSnapshot dataSnapshot) {

    if (!dataSnapshot.exists()) {
      // For some reason, we've reached this point with no dataSnapShot
      Timber.d("dataSnapShot doesnt exists");
      return;
    }

    long timeInSeconds = (System.currentTimeMillis() / 1000);
    mEndedContests = new ArrayList<>();

    long childrenCount = dataSnapshot.getChildrenCount();

    for (DataSnapshot status : dataSnapshot.getChildren()) {

      //fetching the status key for deeper querys inside the db

      final String key = status.getKey();

      checkIfContestEnded(timeInSeconds, key, new OnContestStateReceived() {
        @Override
        public void contestHasEnded() {
          mEndedContests.add(key);
          updateContestCallCounter(childrenCount);
        }

        @Override
        public void contestStillScheduled() {
          updateContestCallCounter(childrenCount);
        }
      });
    }
  }


【问题讨论】:

  • 最明显的原因是导致问题的多线程字段访问。你的代码是在单线程中运行的吗?

标签: android fetch long-integer


【解决方案1】:

完成重新获取数据后,应将所有计数器设置为 0。

【讨论】:

    猜你喜欢
    • 2016-03-11
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多