I have the following code I'm trying to debug and want to add a try/catch block to see why it is crashing. When I add that though, it gives me an unresolved reference a few lines down, as noted in the code comments. What can I do to get around that, please?
private val appServiceInstance by lazy {
// If logs are on app level then it set for everything ..
val configuration =
AppConfiguration.Builder("application-0-rpbsh")
.log(LogLevel.ALL).build()
try{
App.create(configuration) //When try/catch added around this, the issue happens
}
catch (e: Exception) {
Logger.e("Exception caught: " + e.message)
Logger.e(e.stackTraceToString())
}
}
init {
if (!initialized) {
setupRealmSync()
initialized = true
} else {
throw IllegalStateException("Singleton instance already created.")
}
}
private fun setupRealmSync() {
val user = runBlocking { appServiceInstance.currentUser!! } //Unresolved Reference happens here on currentUser
val config = SyncConfiguration
.Builder(
user,
setOf(UserAccount::class,Pellet::class, PelletInsertionVisit::class, PelletLot::class, Patient::class,
Practice::class )
)
.initialSubscriptions(rerunOnOpen = true) { realms ->
add(
realms.query<PelletInsertionVisit>(),
name = "patient info",
updateExisting = true
)
add(realms.query<PelletLot>(), name = "Pallet info", updateExisting = true)
add(realms.query<Patient>(), name = "Patient info", updateExisting = true)
add(realms.query<Practice>(), name = "Practice info", updateExisting = true)
add(realms.query<UserAccount>(), name = "UserAccount info", updateExisting = true)
}
.build()
realm = Realm.open(config)
}
lazy
lambda needs a return value. Previously, it wasApp.create(configuration)
, now it is a combination of that and the result of thecatch
block, which doesn't exist, hence the error. If you re-throw the exception in thecatch
block after logging it should work again:throw e