[英]Why does my loop always go through even though the conditions aren't always met?
I am making a chat app, checking if there are any new messages using a REST call.我正在制作一个聊天应用程序,使用 REST 调用检查是否有任何新消息。 On a one second timer I am checking if the id of the last message in list is the same as the last id of newly downloaded list.
在一秒钟的计时器上,我正在检查列表中最后一条消息的 ID 是否与新下载列表的最后一条 ID 相同。 If it isn't the same id (there are new messages) then update the recycerview.
如果它不是相同的 id(有新消息),则更新 recycerview。 The problem is that it keeps on updating without any new messages and I am not sure why.
问题是它在没有任何新消息的情况下不断更新,我不知道为什么。 Most likely it's a simple problem though i can't seem to find it.
很可能这是一个简单的问题,尽管我似乎找不到它。
Timer:定时器:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
readMessages(myId, chatId);
}
}, 0, 1000);
REST call:休息调用:
private void readMessages(String myId, String chatId) {
apiInterface = ApiClient.getClient().create(userApi.class);
Call<LinkedList<Messages>> call = apiInterface.getMessages(myId, chatId);
call.enqueue(new Callback<LinkedList<Messages>>() {
@Override
public void onResponse(Call<LinkedList<Messages>> call, Response<LinkedList<Messages>> response) {
mList.clear();
mList = response.body();
if (mList2.isEmpty() || mList2.getLast().getId().equals(mList.getLast().getId())) {
messageAdapter = new MessageAdapter(ChatActivity.this, mList, Integer.parseInt(myId));
recyclerView.setAdapter(messageAdapter);
mList2.clear();
mList2 = (LinkedList) mList.clone();
Toast.makeText(ChatActivity.this, mList2.getLast().getId(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LinkedList<Messages>> call, Throwable t) {
}
});
}
The first part of your if
statement is mList2.isEmpty()
(I assume mList
and mList2
are actually the same thing). if
语句的第一部分是mList2.isEmpty()
(我假设mList
和mList2
实际上是同一件事)。 A wild guess for a reason why each call to onResponse
passes if
the test would be that the list is actually empty. if
测试是列表实际上是空的, if
对为什么每次调用onResponse
通过的原因进行了疯狂的猜测。 Try step-by-step debug and placing a breakpoint on the if
line to check, and if so, take a look at your REST service in order to understand why it is responding with an empty list.尝试逐步调试并在
if
行上放置一个断点以进行检查,如果是,请查看您的 REST 服务以了解为什么它以空列表进行响应。
So in your code what exactly is supposed to happen if the condition is not met?那么在您的代码中,如果不满足条件,究竟应该发生什么?
If see that there is an if
statement.如果看到有一个
if
语句。 And let us assume we are not going into it because conditions are not met.让我们假设我们不会进入它,因为条件不满足。 So where is the else statement?
那么 else 语句在哪里呢? What is the code supposed to do if the conditions don't match?
如果条件不匹配,代码应该做什么? As there is nothing else to be done in function, the control will return back from the function to the timer.
由于函数中没有其他事情要做,控件将从函数返回到计时器。
You can probably try putting timer inside the if statement, so it will only run when your conditions are met.您可以尝试将 timer 放在 if 语句中,这样它只会在满足您的条件时运行。
Do you think this was the problem?你认为这是问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.