-
Notifications
You must be signed in to change notification settings - Fork 16
Single launch mode
Sometimes it is required that a given operation must not start if there is a running instance already. The most common example is loading content that needs to be displayed on Activity.
This is how typically Activity loads data.
class MyActivity extends ChronosActivity{
private BusinessObject mData;
protected void onResume(){
super.onResume();
if(mData == null){
loadData();
} else {
showData(); // maps mData to Views
}
}
private void loadData(){
runOperation(new MyOperation());
}
}
However, there is a problem: if Activity riches onResume()
before the loading is complete, there will be started another loading task, that does completely the same job. As a result here we potentially have multiple simultaneously running background tasks, and each of them will end with the same result. Obviously, we would want to not allow such things happen.
In Chronos there is a built-in system, that gives you an opportunity to mark operation runs as "the same" within a single [ChronosConnector] (https://github.com/RedMadRobot/Chronos/blob/master/chronos/src/main/java/com/redmadrobot/chronos/ChronosConnector.java). Is is called tag launches.
To mark two launches as "the same" simply provide a non-null String
tag
object to ChronosConnector runOperation(Operation, String)
method, or the same method in any [pre-defined GUI component] (https://github.com/RedMadRobot/Chronos/tree/master/chronos/src/main/java/com/redmadrobot/chronos/gui). If there would be a request to start another operation with the same tag
it starts only if the previous run with the tag
is not running any more.
Let's see, how does this approach changes the example:
class MyActivity extends ChronosActivity{
private final static String LOADING_DATA_TAG = "loading_data";
private BusinessObject mData;
protected void onResume(){
super.onResume();
if(mData == null){
loadData();
} else {
showData(); // maps mData to Views
}
}
private void loadData(){
runOperation(new MyOperation(), LOADING_DATA_TAG);
}
}
Here it is, when Activity is recreated (after rotation, for example) and reaches loadData
call, Chronos will check, if there is a running operation with tag LOADING_DATA_TAG
, and if there is, no new run will be launched, and the call to runOperation (new MyOperation(), LOADING_DATA_TAG)
will be ignored.