Vincent Williams
08/21/2020, 4:19 PMprivate fun fetchData() = viewModelScope.launch(dispatcher) {
repository.fetchData()
.onEach {
//success
}
.catch { Timber.e(it) }
.launchIn(this)
}
Why is this still crashing my app when an exception occurs? should catch be catching that and not crashing?Zach Klippenstein (he/him) [MOD]
08/21/2020, 4:27 PMVincent Williams
08/21/2020, 4:28 PMVincent Williams
08/21/2020, 4:28 PMcatch would be equivalentZach Klippenstein (he/him) [MOD]
08/21/2020, 4:29 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 4:30 PMcatch and see if it's even getting the exception?Vincent Williams
08/21/2020, 4:30 PMVincent Williams
08/21/2020, 4:31 PMVincent Williams
08/21/2020, 4:31 PMfetchData network call so catch should definitely be catching that I think...Vincent Williams
08/21/2020, 4:33 PMFATAL EXCEPTION: DefaultDispatcher-worker-3 before crashingZach Klippenstein (he/him) [MOD]
08/21/2020, 4:35 PMVincent Williams
08/21/2020, 4:35 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 4:36 PMcollect directly instead of onEach/launchIn, and surround the collect with a try/catch)Zach Klippenstein (he/him) [MOD]
08/21/2020, 4:37 PMVincent Williams
08/21/2020, 4:43 PMonEach instead of collect because I had some cases where I needed multiple collects in one launch
private fun subscribeToViewModel() = lifecycleScope.launch(Dispatchers.Main) {
viewModel.viewState.collect { onViewStateEvent(it) }
viewModel.navigationEvents.collect { onNavigationEvent(it) }
}Vincent Williams
08/21/2020, 4:43 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 4:43 PMnavigationEvents won’t start collecting until viewState finishes.Zach Klippenstein (he/him) [MOD]
08/21/2020, 4:44 PMviewModel.viewState.onEach{…}.launchIn(lifecycleScope), you don’t need the outer call to launch{}Vincent Williams
08/21/2020, 4:45 PMVincent Williams
08/21/2020, 4:48 PMVincent Williams
08/21/2020, 4:49 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 5:00 PMfetchData shouldn’t be a suspend function if it’s returning a Flow.Vincent Williams
08/21/2020, 5:04 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 5:05 PMVincent Williams
08/21/2020, 5:05 PMVincent Williams
08/21/2020, 5:05 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 5:05 PMVincent Williams
08/21/2020, 5:08 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 5:09 PMflow {
val data = fetchData() // suspending call
storeDataInDb(data) // suspending call
val dbStream: Flow<…> = streamDataFromDb() // not suspending
emitAll(dbStream)
}Vincent Williams
08/21/2020, 5:11 PMstreetsofboston
08/21/2020, 5:15 PMcatch handles an exception thrown by the returned Flow. From this thread, I understand that the repository.fetchData() throws the exception? If that's the case, the catch won't handle this, since it'll only handle exceptions from the returned Flow; no Flow is returned, an exception is thrown instead inside the viewModelScope.launch call.Zach Klippenstein (he/him) [MOD]
08/21/2020, 5:19 PMfetchData was throwing the exception directly.Vincent Williams
08/21/2020, 5:34 PMflow {
val data = fetchData() // suspending call
storeDataInDb(data) // suspending call
val dbStream: Flow<…> = streamDataFromDb() // not suspending
emitAll(dbStream)
}Vincent Williams
08/21/2020, 5:37 PMZach Klippenstein (he/him) [MOD]
08/21/2020, 6:19 PM