Question

My use case is that whenever an user types something an EditText, the input data is used for performing operations on the background. These operations might take long enough to cause an ANR. @TextChange together with @Background works fine if the operation is done quicly enough. But is the operation takes long enough, so that the user inputs more data, I will get threading issues as there will be multiple background tasks that will command the update of same UI component.

I think I achieve the wanted behaviour with AsyncTask API, but wanted to look for AndroidAnnotations based solutions as well, as it simplifies the code a lot. Great lib by the way.

Below are some code snippets that'll hopefully illustrate my point. Thanks for at least reading, comments/answers appreciated :)

@TextChange
void onUserInput(...) {
  // This will start a new thread on each text change event
  // thus leading to a situation that the thread finishing
  // last will update the ui
  // the operation time is not fixed so the last event is
  // not necessary the last thread that finished
  doOperation()
}

@Background
void doOperation() {
  // Sleep to simulate long taking operation
  Thread.sleep( 6000 );
  updateUi()
}

@UiThread
void updateUi() {
  // Update text field etc content based on operations
}

UPDATE: This is not possible at the moment, see DayS' answer below.

Était-ce utile?

La solution

It's possible since AA 3.0. Read this thread.

@Override
protected void onStop() {
    super.onStop();
    boolean mayInterruptIfRunning = true;
    BackgroundExecutor.cancelAll("longtask", mayInterruptIfRunning);
}

@Background(id="longtask")
public void doSomethingLong() {
    // ...
}

Autres conseils

There already was this kind of request on Android Annotations but it was closed because no solution was proposed. But if you have any idea about it, go ahead and re-open this issue ;)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top