bobby
12/28/2020, 4:04 AMsingle{}
? The reason I’m asking this is because when creating EncryptedSharedPreferences
object using single{}
, sometimes it’s throw an Exception, and I want to catch that, prevent the crash app on user side, thanksGiorgos Neokleous
12/28/2020, 11:47 AMsingle<EncryptedSharedPreferences?> {
runCatching {
// Create object
}.getOrNull()
}
Never tried koin with nullable objects but I don't see a reason not to work. I would suggest to think it through though as not crashing means that you can't make any assumptions that the object exists thoughout the app, meaning crashing might be the best course of action.
The other alternative is to create a "factory" that catches the exception and provides an alternative object.bobby
12/29/2020, 1:44 AMsingle<SharedPreferences> {
try {
//return EncryptedSharedPreference object
} catch (e: Exception) {
//Send to firebase crashlytics
//return SharedPreference object
}
}
I think it’s the same rather make it return null
or SharedPreference
object, it’s prevent the crash though. Yeah I already think of it thoroughly, because the code is fine, but the library itself is the problem, AFAIK, thanksbobby
12/29/2020, 1:45 AMThe other alternative is to create a “factory” that catches the exception and provides an alternative object.what’s the difference using
factory
and single
? I don’t see any differenceGiorgos Neokleous
12/29/2020, 8:27 AMbobby
12/29/2020, 8:54 AMbobby
12/29/2020, 9:10 AMfun provideEncryptedSharedPreference(): SharedPreferences? {
return try {
EncryptedSharedPreferences.create()
} catch (e: Exception) {
null
}
}
single() {
return@single provideEncryptedSharedPreference()
}
single() {
//instantiate helper class
Helper(getOrNull())
}
bobby
12/29/2020, 9:11 AMrunCatching
can’t add firebase crashlytics record exception in it’s catch
. still want to record the exception in case some other exception happens