简体   繁体   中英

Using 2 nested asynchronous threads - Android Java AutoCompleteTextView

I've been working on creating a dynamic AutoCompleteTextView , and encountered a problem using two nested asynchronous threads.

The first asynchronous thread is a Filter , and inside this thread I make HTTP calls asynchronously. That causes the filter to return empty FilterResults because Filter doesn't wait for asynchronous HTtP calls.

To overcome this situation I used a flag, and used a while loop inside the filter just before the return line waiting for asynchronous HTTP calls to finish and set the flag.

  • This worked fine, but is there a better solution?

  • Can this approach lead to another problem that I can't see?

I was using JSON requests asynchronously, so I overcome this situation by using synchronous JSON requests like following:

RequestFuture<JSONObject> future = RequestFuture.newFuture();
itemReq = new JsonObjectRequest(url + city, new JSONObject(), future, future);
AppController.getInstance().addToRequestQueue(itemReq);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM