简体   繁体   English

为什么我的循环总是通过,即使条件并不总是满足?

[英]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() (我假设mListmList2实际上是同一件事)。 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.

相关问题 如果不满足if语句中的条件,为什么while循环中的else语句不执行? - Why won't the else statement in my while loop execute when the conditions in the if statement aren't met? 如果满足条件,语句是否也不会总是消失? - If statement doesn't always go off even when conditions are meet? 为什么即使我编写的代码不会产生零或重复的数字,我的程序的输出也总是以两个连续的零开头? - Why does the output for my program always start with two consecutive zeros even though I wrote a code that doesn't produce zeros or repeated numbers? 为什么我的代码总是转到else语句? - Why does my code always go to the else statement? 为什么 JFrame 对象似乎还活着,即使没有对它的引用? - Why does the JFrame object seem to stay alive, even though there aren't references to it? 即使满足条件也不会抛出异常 - Exception is not thrown even though conditions are met 为什么默认构造函数总是出现在javadoc文档中,即使代码中不存在它? - Why does the default constructor always appear in javadoc documentation, even though it is not present in the code? 为什么我的CheckTypeRoomForGuest()方法不循环所有来宾并且总是为true? - Why does my CheckTypeRoomForGuest() methods not loop all guest and always true? 即使条件为真,为什么我的方法仍返回假? - Why is my method returning false even though the conditions are true? 即使符合条件,我的While循环也不会结束 - My While loop doesn't end even when the condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM