【发布时间】: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