Without seeing your code, it's hard to say what might happen. But the first thing that comes to mind is that your network request is in a different thread, and therefore your performFiltering() may return an empty result set prematurely. At this point, publishResults() returns an empty result, and your drop-down list is empty. Later, your AsyncTask will return its result and add the results to the list of adapters, but for one reason or another it is not yet displayed.
I think you may be mistaken in the need to use AsyncTask. The Filter object already does something similar to AsyncTask: performFiltering() is executed in the background thread, and publishResults() is called from the user interface thread after the completion of the Filtering () function. Thus, you can execute your network request directly in the performFiltering () function and set the results in the FilterResults object, and you donβt have to worry about the network request being too slow and causing problems in the user interface.
An alternative solution, which is a little more complicated, but what I am doing in the Filter object (due to the existing architecture that calls the API in the background using an asynchronous callback instead of the blocking / synchronous step as needed for performFiltering ()) is using a synchronized object with the wait () / notify () function to perform cross-flow control, therefore the effect is the same as when executing a network request directly in performFiltering (), but it actually occurs in several topics:
However, I think you may find that the simplest solution is to simply execute the network request synchronously, directly in the performFiltering () method. The above code example is just one option if you already have an architecture for asynchronous / API callbacks and you don't want to change this behavior to get synchronous results in performFiltering ().
Joe
source share