Paul Woitaschek
02/26/2017, 12:12 PMelizarov
02/26/2017, 12:17 PMBookAdder.scannerActive
obervable value?Paul Woitaschek
02/26/2017, 12:18 PMelizarov
02/26/2017, 12:21 PMstopScanner
flag? I cannot follow the logic. There is only one place where it is set to true
, but then immediately to false
again...Paul Woitaschek
02/26/2017, 12:26 PMPaul Woitaschek
02/26/2017, 12:26 PMsingleThreadExecutor
Paul Woitaschek
02/26/2017, 12:26 PMPaul Woitaschek
02/26/2017, 12:27 PMprivate fun throwIfStopRequested() {
if (stopScanner) {
throw InterruptedException("Interruption requested")
}
}
Paul Woitaschek
02/26/2017, 12:29 PMInterruptedException
was caught it sets the flag to false because the single thread from the executor completedelizarov
02/26/2017, 1:19 PMobserveOn(AndroidSchedulers.mainThread())
without the need to work with loopers explicity.elizarov
02/26/2017, 1:26 PMval executor = Executors.newSingleThreadExecutor()
with val context = newSingleThreadContext()
and replace executor.execute { ... }
with launch(context) { ... }
. You get the job, that you can cancel, so now you'll need to keep a reference to the last job:
fun scanForFiles(interrupting: Boolean) {
if (job?.isActive != true || interrupting) {
job?.cancel()
job = launch(Context) {
...
}
}
Instead of throwIfStopRequested
you can just use yield
. Saves you a few lines, but all in all it does not get really much different.Paul Woitaschek
02/26/2017, 1:34 PMPaul Woitaschek
02/26/2017, 1:35 PMobserveOn
thing: Often times its easier to just stay on the ui threadPaul Woitaschek
02/26/2017, 2:24 PMelizarov
02/26/2017, 2:35 PMPaul Woitaschek
02/26/2017, 2:52 PMelizarov
02/26/2017, 2:59 PMPaul Woitaschek
02/26/2017, 3:13 PMPaul Woitaschek
02/26/2017, 3:14 PM