Skip to content

Commit 8930055

Browse files
authored
Merge pull request #83 from thefuntasty/fix/coroutines-interactor-crash
Fix: possible crash from race condition
2 parents 5162613 + bb64a22 commit 8930055

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

cr-interactors/src/main/java/com/thefuntasty/mvvm/crinteractors/CoroutineScopeOwner.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,25 @@ interface CoroutineScopeOwner {
4242
onSuccess: (T) -> Unit,
4343
onError: ((Throwable) -> Unit)?
4444
) {
45-
deferred?.cancel()
46-
deferred = coroutineScope.async(getWorkerDispatcher()) {
47-
build()
48-
}.also {
49-
coroutineScope.launch(Dispatchers.Main) {
50-
try {
51-
onSuccess(it.await())
52-
} catch (error: Throwable) {
53-
onError?.invoke(error) ?: throw error
45+
try {
46+
deferred?.cancel()
47+
deferred = coroutineScope.async(getWorkerDispatcher()) {
48+
build()
49+
}.also {
50+
coroutineScope.launch(Dispatchers.Main) {
51+
try {
52+
onSuccess(it.await())
53+
} catch (error: Exception) {
54+
onError?.invoke(error) ?: throw error
55+
}
5456
}
5557
}
58+
} catch (cancellation: CancellationException) {
59+
60+
} catch (error: Exception) {
61+
onError?.invoke(error) ?: throw error
5662
}
63+
5764
}
5865

5966
/**

0 commit comments

Comments
 (0)