Fatal Spin-On-Suspend/Stuck on ThreadID

后端 未结 2 1470
天涯浪人
天涯浪人 2021-01-02 02:29

I\'m creating a custom calendar for an Android app. The way it works now is that it pulls events from an online MySQL database, transfers them into a JSONArray, and inputs t

相关标签:
2条回答
  • 2021-01-02 03:05

    I went about this a slightly different way:

    First I set a field in the AsyncTask class

    private boolean taskDone = false;
    

    Then, after the doInBackground code (or in postExecute):

    this.taskDone = true;
    

    Finally add a method to the AsyncTask:

    public boolean isTaskDone() {
        return taskDone;
    }
    

    Now you can use isTaskDone as the argument in the while loop:

    while (!isTaskDone()) {
    // wait for task
    }
    

    Just my way of doing it, I'm sure there are many more

    0 讨论(0)
  • 2021-01-02 03:09

    I figured it out after another day of playing around with it. It was timing out because of these lines in the non-AsyncTask:

    while (db.result == null) {
        continue;
    }
    

    It took this as "you're caught forever in a while loop and therefore the program must have crashed", when in reality it was just taking time to read the events. However, without that there it would just move on without having fully loaded the events, creating a NullPointerException when it tries to use that array. What I did instead was do this:

    while (db.result == null){
       try {
          Thread.sleep(100);
       } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
       continue;
    }
    

    The sleeping seems to send back to the app that I'm still there, just waiting for the data so I can continue. Either that or it slows down the amount of times I go through the while loop. Not sure which one it does but either way it solves the problem.

    0 讨论(0)
提交回复
热议问题