Is failure to call Continuation.resumeX() necessarily a problem?When using kotlin coroutines, how do I unit test a function that calls a suspend function?Kotlin suspend function recursive callHow would I “wrap” this not-quite-“by lazy” result caching function call in idiomatic Kotlin?How does Kotlin coroutines know when to yield when making network calls?Asynchronous http calls with coroutines (using an http client like Feign)Android Room Kotlin - Query in background thread - return value problemCall Kotlin suspend function in Java classSpring Boot Kotlin Coroutine Caching of Http CallsKotlin Coroutines: Calling Deferred::await in Sequence::mapUse coroutines to update UI while making network call
When leasing/renting out an owned property, is there a standard ratio between monthly rent and the mortgage?
Is it OK to bring delicacies from hometown as tokens of gratitude for an out-of-town interview?
Is there a rule that prohibits us from using 2 possessives in a row?
Concise way to draw this pyramid
How can Iron Man's suit withstand this?
Can a helicopter mask itself from radar?
Restoring order in a deck of playing cards (II)
Accidentally cashed a check twice
PhD student with mental health issues and bad performance
How is it possible for Mordenkainen to be alive during the Curse of Strahd adventure?
Why is Colorado so different politically from nearby states?
Short story written from alien perspective with this line: "It's too bright to look at, so they don't"
Please help me identify this plane
Is having a hidden directory under /etc safe?
Asking bank to reduce APR instead of increasing credit limit
Word for a small burst of laughter that can't be held back
How did rebel fighters get past the Scarif shield?
Is /home directory in root partition mapped to /home partition
Setting extra bits in a bool makes it true and false at the same time
Access to all elements on the page
Why use water tanks from a retired Space Shuttle?
Computing the differentials in the Adams spectral sequence
Is this cancel button needed?
Story about a toddler with god-like powers, dangerous tantrums
Is failure to call Continuation.resumeX() necessarily a problem?
When using kotlin coroutines, how do I unit test a function that calls a suspend function?Kotlin suspend function recursive callHow would I “wrap” this not-quite-“by lazy” result caching function call in idiomatic Kotlin?How does Kotlin coroutines know when to yield when making network calls?Asynchronous http calls with coroutines (using an http client like Feign)Android Room Kotlin - Query in background thread - return value problemCall Kotlin suspend function in Java classSpring Boot Kotlin Coroutine Caching of Http CallsKotlin Coroutines: Calling Deferred::await in Sequence::mapUse coroutines to update UI while making network call
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm using suspendCoroutine
to avoid using callbacks in Dialog
s. However, in Android Dialog
s there is no obvious place to call Continuation.resume()
when the dialog is dismissed (by clicking outside of the dialog area). If you attempt the call in Dialog.setOnDismissListener()
then you have to keep track of whether resume was already called in the button listener.
suspend fun displayDialog() = suspendCoroutine<String?> continuation ->
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
continuation.resume(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
continuation.resume("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
// if the user clicked on OK, then resume has already been called
// and we get an IllegalStateException
continuation.resume(null)
So, is it better to keep track of whether resume was already called (to avoid calling it a second time), or just don't bother with the resume(null)
call (in onDismissListener)?
kotlin android-dialog kotlin-coroutines
add a comment |
I'm using suspendCoroutine
to avoid using callbacks in Dialog
s. However, in Android Dialog
s there is no obvious place to call Continuation.resume()
when the dialog is dismissed (by clicking outside of the dialog area). If you attempt the call in Dialog.setOnDismissListener()
then you have to keep track of whether resume was already called in the button listener.
suspend fun displayDialog() = suspendCoroutine<String?> continuation ->
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
continuation.resume(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
continuation.resume("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
// if the user clicked on OK, then resume has already been called
// and we get an IllegalStateException
continuation.resume(null)
So, is it better to keep track of whether resume was already called (to avoid calling it a second time), or just don't bother with the resume(null)
call (in onDismissListener)?
kotlin android-dialog kotlin-coroutines
add a comment |
I'm using suspendCoroutine
to avoid using callbacks in Dialog
s. However, in Android Dialog
s there is no obvious place to call Continuation.resume()
when the dialog is dismissed (by clicking outside of the dialog area). If you attempt the call in Dialog.setOnDismissListener()
then you have to keep track of whether resume was already called in the button listener.
suspend fun displayDialog() = suspendCoroutine<String?> continuation ->
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
continuation.resume(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
continuation.resume("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
// if the user clicked on OK, then resume has already been called
// and we get an IllegalStateException
continuation.resume(null)
So, is it better to keep track of whether resume was already called (to avoid calling it a second time), or just don't bother with the resume(null)
call (in onDismissListener)?
kotlin android-dialog kotlin-coroutines
I'm using suspendCoroutine
to avoid using callbacks in Dialog
s. However, in Android Dialog
s there is no obvious place to call Continuation.resume()
when the dialog is dismissed (by clicking outside of the dialog area). If you attempt the call in Dialog.setOnDismissListener()
then you have to keep track of whether resume was already called in the button listener.
suspend fun displayDialog() = suspendCoroutine<String?> continuation ->
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
continuation.resume(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
continuation.resume("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
// if the user clicked on OK, then resume has already been called
// and we get an IllegalStateException
continuation.resume(null)
So, is it better to keep track of whether resume was already called (to avoid calling it a second time), or just don't bother with the resume(null)
call (in onDismissListener)?
kotlin android-dialog kotlin-coroutines
kotlin android-dialog kotlin-coroutines
asked Mar 24 at 11:41
MarkMark
4,57544159
4,57544159
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Continuation is a low-level primitive that shall be resumed exactly once, so you have to track whether resume
was already called when using it. Alternatively, you can use higher-level communication primitives, for example CompletableDeferred
, which has multi-use complete
function:
suspend fun displayDialog(): String?
val deferred = CompletableDeferred<String?>()
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
deferred.complete(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
deferred.complete("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
deferred.complete(null)
return deferred.await()
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
A coroutine might potentially keep resources. For example, if a coroutine opens a file and callsdisplayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.
– Roman Elizarov
Mar 25 at 15:10
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323411%2fis-failure-to-call-continuation-resumex-necessarily-a-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Continuation is a low-level primitive that shall be resumed exactly once, so you have to track whether resume
was already called when using it. Alternatively, you can use higher-level communication primitives, for example CompletableDeferred
, which has multi-use complete
function:
suspend fun displayDialog(): String?
val deferred = CompletableDeferred<String?>()
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
deferred.complete(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
deferred.complete("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
deferred.complete(null)
return deferred.await()
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
A coroutine might potentially keep resources. For example, if a coroutine opens a file and callsdisplayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.
– Roman Elizarov
Mar 25 at 15:10
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
add a comment |
Continuation is a low-level primitive that shall be resumed exactly once, so you have to track whether resume
was already called when using it. Alternatively, you can use higher-level communication primitives, for example CompletableDeferred
, which has multi-use complete
function:
suspend fun displayDialog(): String?
val deferred = CompletableDeferred<String?>()
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
deferred.complete(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
deferred.complete("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
deferred.complete(null)
return deferred.await()
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
A coroutine might potentially keep resources. For example, if a coroutine opens a file and callsdisplayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.
– Roman Elizarov
Mar 25 at 15:10
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
add a comment |
Continuation is a low-level primitive that shall be resumed exactly once, so you have to track whether resume
was already called when using it. Alternatively, you can use higher-level communication primitives, for example CompletableDeferred
, which has multi-use complete
function:
suspend fun displayDialog(): String?
val deferred = CompletableDeferred<String?>()
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
deferred.complete(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
deferred.complete("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
deferred.complete(null)
return deferred.await()
Continuation is a low-level primitive that shall be resumed exactly once, so you have to track whether resume
was already called when using it. Alternatively, you can use higher-level communication primitives, for example CompletableDeferred
, which has multi-use complete
function:
suspend fun displayDialog(): String?
val deferred = CompletableDeferred<String?>()
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setNegativeButton(android.R.string.cancel) _, _ ->
deferred.complete(null)
builder.setPositiveButton(android.R.string.ok) _, _ ->
deferred.complete("it's ok")
val dialog = builder.show()
dialog.setOnDismissListener
deferred.complete(null)
return deferred.await()
answered Mar 24 at 14:16
Roman ElizarovRoman Elizarov
9,74642941
9,74642941
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
A coroutine might potentially keep resources. For example, if a coroutine opens a file and callsdisplayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.
– Roman Elizarov
Mar 25 at 15:10
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
add a comment |
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
A coroutine might potentially keep resources. For example, if a coroutine opens a file and callsdisplayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.
– Roman Elizarov
Mar 25 at 15:10
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
Ok, thanks that's very useful. But I'm still curious what are the implications of not calling resume at all? For example, if the caller does not need to do any work when a dialog is dismissed and so potentially does not need to know this event. Are such paused coroutines using up resources?
– Mark
Mar 24 at 14:37
A coroutine might potentially keep resources. For example, if a coroutine opens a file and calls
displayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.– Roman Elizarov
Mar 25 at 15:10
A coroutine might potentially keep resources. For example, if a coroutine opens a file and calls
displayDialog
, then it will not close the file if the coroutines is not resumed. Moreover, coroutine debugging facilities keep a list of all coroutines, so if a coroutine is not resumed then it is stuck in that list forever -- memory leak.– Roman Elizarov
Mar 25 at 15:10
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
Yes, regarding an open File, that's a case where the caller does need to do something (close the file), so I'm talking about the cost of being stuck in that list. Won't it be removed when the job is cancelled?
– Mark
Mar 27 at 3:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323411%2fis-failure-to-call-continuation-resumex-necessarily-a-problem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown