Check whether a @Background task is running #1909
Comments
@Background
task is running
As far as i know there is currently no (public) build-in method for checking this. and i'm not sure if making As a approach until there is a possible solution you could either use a flag field, a callback (that gets notified of start/stop of the task) passed as e.g. param or some start/stop events with EventBus. @WonderCsabo wdyt would it be enough and safe to make |
Thanks for your interest ! I thought about managing a flag myself, but here are cases where I suspect it would quickly become tedious to handle, or the flag would become stale. I'm canceling the task from other UI callbacks, and I'm quite unsure about what happens if the activity gets suspended because it loses focus while the task is running. I could probably handle that with I'm not in any hurry to release, I aim to learn and I can take my time to get things right ™. It looks like the I'm quite new to Java and Android, and this is my first partial reading of AA's code, so I may totally be wrong. |
Never mind my previous comment ; the task looks like it's re-added afterwards to |
I'm looking into the unit-tests for BackgroundExecutor. I found ThreadActivityTest but I'm even more confused... Now I remember why I stay as far away from multithreading as I can. While we're waiting for @WonderCsabo I'll go ahead and make (much later) On another (somewhat unrelated) note, I followed the instructions on how to set up the dev env for this lib, and hit one unmentioned snag : setting the path to the I solved it with the env var, as it suggests
Also, maybe it should be mentioned somewhere in that wiki page that java 1.8 is required. I remember seeing it mentioned elsewhere, but it's not where it matters. Nothing stackoverflow can't help you solve, still... Other than that, congratulations on a smooth setup process ! It's so satisfying to see it do everything ! |
I went ahead and tried it out. The tests are really ugly, but they pass. I'm not sure how to write them better ; I kind of copy-pasted and monkey-coded until the following was successful.
🙈 🙊 🙉 Also, looking through the tests, I'm pretty sure that assertion is useless, unless there's more black magic at play. |
Hey @Goutte, from reading the test it looks okay i think... but I'm not that deep into the multithreading "behind the scenes"... :D May @WonderCsabo and possibly even @rom1v (he implemented the serial support) can check that too? This would be a nice feature. :D |
Thanks for the interest, @dodgex ! I'm actually right now trying my fork into the field, and I can tell you it's not working as expected ! I'm reviewing the |
The problem is that it is inhenrently racy. Suppose that such a method is available, let's call it if (isRunning(…)) {
// do something assuming the given task is running
} else {
// do something assuming the given task is not running
} In that case, the assumptions may be incorrect: for example, immediately after Could you explain a bit more your concrete need? |
What I did before moving to annotations was (given I have an I am then using this condition in my The FAB acts both as a launcher for the async (which is a rather long http request) and also as a cancel button while the request is still running. But there are other ways to launch the async, such as when geolocation is resolved, which can happen pretty much anytime, and I want the FAB to be visually updated accordingly. I have the same racing problem with my current implementation as I would have with I need to think about this some more, and even run multiple threads of thought. Time for a walk. Thanks for the eye-opener. |
There's something bugging me : @Background(serial = "request")
void doSomeRequest() {
// Request something via the RestClient
// ...
updateUi(); // ain't the bg thread guaranteed to be finished when we reach here ?
}
@UiThread
void updateUi() {
if (BackgroundExecutor.hasSerialRunning("request")) {
// make FAB a cancel button
} else {
// make FAB a request button
}
} If I launch other Also, I'm a idiot. The |
I'm looking some more at the generated sources. Any method with |
The this means: that if you for example continue to do some work in your background method after calling I think in most cases |
In your case
|
@Goutte regarding your notes #1909 (comment) on the instructions:
|
Thanks @dodgex ; I groked it. My evening-meditation line of thinking was, if the @WonderCsabo, about Java |
@Goutte i see thanks. Actually we use Java 1.7 for compiling the code, but it seems some of the Maven plugins or Maven itself now requires Java 1.8. I will update the instructions. |
Also, I had not seen the issue #979 . Referencing it here. |
I can't find how to do this with AA. Is it even doable ?
Checking either by
serial
orid
is fine by me, even if it does not actually mean the same thing, as far as I understand.I found the
BackgroundExecutor.hasSerialRunning(String serial)
method, but it's private.I'm trying to hack around with
checkBgThread
and a customWrongThreadListener
but it feels overly complicated and anyhow I want to run this check from the UI thread so I'm not sure this'll work.I'm probably doing this wrong, I can't be the only one with this need ; I'll update the doc if there's a simple solution I've overlooked.
To clarify, I want to check the state of a specific background task in response to user input (a click on a button) and in event listeners (I'm using the EventBus). It was rather easily doable with Asyncs, but I'm trimming the fat with the awesome annotations.
The text was updated successfully, but these errors were encountered: