bookmate game
en

Mark Murphy

  • b4777467766membuat kutipan2 tahun yang lalu
    Similarly, coroutines are cooperative with respect to cancellation. It is entirely possible to create a coroutine that is oblivious to a cancellation request, but that is not a particularly good idea.
  • b4777467766membuat kutipan2 tahun yang lalu
    yield() does two things:

    Since it is a suspend function, it signals to the coroutines system that it is safe to switch to another coroutine from this dispatcher, if there is one around that is ready to run
    It throws CancellationException if the job was canceled
  • b4777467766membuat kutipan2 tahun yang lalu
    Another option for code with access to a CoroutineScope is to check the isActive property
  • b4777467766membuat kutipan2 tahun yang lalu
    Sometimes, you have code that simply cannot afford to be canceled. This should be the exception, not the rule… which is why the best example of this scenario is a catch or finally block. If you are cleaning things up from a crash, or are freeing resources from some work in a try block, you want all of your work to proceed, even if the job you are in had been canceled already.

    To do this, wrap your code in a withContext(NonCancellable) block. This ignores any prior cancellations.
  • b4777467766membuat kutipan2 tahun yang lalu
    If you need to know whether your job has been canceled from some specific piece of code inside of the job, you can catch the CancellationException. Alternatively, you could catch a specific subclass, such as TimeoutCancellationException, as we did in one of the withTimeout() samples
  • b4777467766membuat kutipan2 tahun yang lalu
    You will be passed a cause value that will be:

    null if the job completed normally
    a CancellationException (or subclass) if the job was canceled
    some other type of exception if the job failed
  • b4777467766membuat kutipan2 tahun yang lalu
    So, mostly, this is for lightweight cleanup or possibly some form of error logging
  • b4777467766membuat kutipan2 tahun yang lalu
    The three main ways to get a Flow are:

    Calling a top-level function from the Kotlin coroutines system,
    Converting a Channel to a Flow, or
    Getting a Flow from a third-party library
  • b4777467766membuat kutipan2 tahun yang lalu
    flowOf() might seem pointless. After all, if we already have the objects, why do we need to bother with a Flow? However, there are a few cases where that is indeed what we want, such as:

    Testing, where we need to provide a Flow and we already have our test data to provide
    Error conditions, where we have our error object already, but the API that we are implementing requires a Flow (perhaps of a sealed class representing loading/content/error states)
  • b4777467766membuat kutipan2 tahun yang lalu
    emptyFlow() is basically flowOf() with no parameters. It returns a flow that is already closed, so a collect() call will just return immediately.
fb2epub
Seret dan letakkan file Anda (maksimal 5 sekaligus)