Tower Guidev2
03/09/2023, 4:32 PMoverride suspend fun displayItem(contentId: Long): DisplayItemState =
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val encodedUrl = encode("${BuildConfig.MY_CONTENT_C_URL}$contentId", StandardCharsets.UTF_8.name())
DisplayItemState(Intent(Intent.ACTION_VIEW, Uri.parse("${BuildConfig.MY_MOBILE_CLICK}${loginToken()!!}?url=$encodedUrl")))
}
init{}
of my viewModel
private lateinit var notificationDisplayItem: DisplayItemState
init {
if (isNotification) viewModelScope.launch {
when (notificationAction) {
VALUE_ARG_SPECIALTY_NOTIFICATION_ACTION_VIEW -> notificationDisplayItem = repository.displayItem(contentId!!)
VALUE_ARG_SPECIALTY_NOTIFICATION_ACTION_SAVE -> TODO("Should never occur as save broadcast receiver")
VALUE_ARG_SPECIALTY_NOTIFICATION_ACTION_DISCARD -> TODO("Should never occur as discard broadcast receiver")
else -> TODO("Unexpected notification action $notificationAction")
}
}
}lateinit
variable notificationDisplayItem
is not being initialized
how can i get my code to "wait" for the call to repository.displayItem(contentId!!)
to respondSam
03/09/2023, 4:46 PMdisplayItem
need to suspend, though?Tower Guidev2
03/09/2023, 4:48 PMPeter Farlow
03/09/2023, 4:49 PMSam
03/09/2023, 4:50 PMTower Guidev2
03/09/2023, 4:50 PMdoes the function loginToken() also suspend? If so, displayItem needs to suspendyes it does
encode
call with "P_*ossibly blocking call in non-blocking context could lead to thread starvation*_"encode
is not that big an issueSam
03/09/2023, 5:02 PMencode
function coming from? There are a few things that could cause that warning, e.g. declaring certain types of exceptionTower Guidev2
03/09/2023, 5:05 PMimport java.net.URLEncoder.encode
Sam
03/09/2023, 5:08 PMUnsupportedEncodingException
, which extends IOException
. The IDE sees that and assumes that IOException = IO = blocking, which is a pretty big leap 😄Tower Guidev2
03/09/2023, 5:09 PMRobert Williams
03/10/2023, 10:34 AMprivate val notificationDisplayItem: Deferred<DisplayItemState> = viewModelScope.async {//suspending work}
where it's needed:
notificationDisplayItem.await() //returns result, suspends or throws