0

I am using Android Annotations in my project.

I am performing a set of tasks in background threads. Each background thread contains a call to a REST endpoint(using Retrofit). All calls are synchronous at this point. I am trying to cancel all threads if I leave the app(in onDestroy of the fragment).

My code is structured something like this

@Background
void bgMethod1(){
//call rest endpoint get response
uiThread1(response)
}

@UiThread
void uiThread1(){
some UI related changes = show toasts etc
bgMethod2();
}

@Background
void bgMethod2()
//call next rest endpoint get response
uiThread2(response)
}

 @UiThread
void uiThread2(){
some UI related changes = show toasts etc
bgMethod3();
}

And so on and so forth... At any point while these threads are running, if I quit the app(press the back button), I want to stop all these threads from running(stop the current REST call as well as all future calls which )

I am currently referring to this github page for Android annotations

https://github.com/excilys/androidannotations/wiki/WorkingWithThreads

I have tried to use BackgroundExecutor.cancelAll inonDestroy of the fragment to no avail. All the methods execute(all REST calls are made sequentially).

How do I stop all the threads from executing/cancel as well as stop the requests, both ongoing and future, made by Retrofit in these threads.

All the methods in my Retrofit REST interface are synchronous and have a return type.

Rohan
  • 593
  • 7
  • 22

1 Answers1

0

First, you should add a cancellation identifier to your annotations.

@Background(id = "someId")
void bgMethod1(){
  //call rest endpoint get response
  uiThread1(response)
}

If you want to interrupt the already running requests, you can do this:

BackgroundExecutor.cancelAll("someId", true)

However this may result in stale requests to your server...

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
  • Well, if a task is already executing, it will [interrupt](http://stackoverflow.com/a/3590008/747412) the thread. This does not mean the thread will stop. If the task is not started yet, it will be definitely cleared from execution. – WonderCsabo Sep 02 '15 at 13:30