1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines 6 7 /** 8 * Base class for special [CoroutineDispatcher] which is confined to application "Main" or "UI" thread 9 * and used for any UI-based activities. Instance of `MainDispatcher` can be obtained by [Dispatchers.Main]. 10 * 11 * Platform may or may not provide instance of `MainDispatcher`, see documentation to [Dispatchers.Main] 12 */ 13 public abstract class MainCoroutineDispatcher : CoroutineDispatcher() { 14 15 /** 16 * Returns dispatcher that executes coroutines immediately when it is already in the right context 17 * (e.g. current looper is the same as this handler's looper) without an additional [re-dispatch][CoroutineDispatcher.dispatch]. 18 * 19 * Immediate dispatcher is safe from stack overflows and in case of nested invocations forms event-loop similar to [Dispatchers.Unconfined]. 20 * The event loop is an advanced topic and its implications can be found in [Dispatchers.Unconfined] documentation. 21 * The formed event-loop is shared with [Unconfined] and other immediate dispatchers, potentially overlapping tasks between them. 22 * 23 * Example of usage: 24 * ``` 25 * suspend fun updateUiElement(val text: String) { 26 * /* 27 * * If it is known that updateUiElement can be invoked both from the Main thread and from other threads, 28 * * `immediate` dispatcher is used as a performance optimization to avoid unnecessary dispatch. 29 * * 30 * * In that case, when `updateUiElement` is invoked from the Main thread, `uiElement.text` will be 31 * * invoked immediately without any dispatching, otherwise, the `Dispatchers.Main` dispatch cycle will be triggered. 32 * */ 33 * withContext(Dispatchers.Main.immediate) { 34 * uiElement.text = text 35 * } 36 * // Do context-independent logic such as logging 37 * } 38 * ``` 39 * 40 * Method may throw [UnsupportedOperationException] if immediate dispatching is not supported by current dispatcher, 41 * please refer to specific dispatcher documentation. 42 * 43 * [Dispatchers.Main] supports immediate execution for Android, JavaFx and Swing platforms. 44 */ 45 public abstract val immediate: MainCoroutineDispatcher 46 47 /** 48 * Returns a name of this main dispatcher for debugging purposes. This implementation returns 49 * `Dispatchers.Main` or `Dispatchers.Main.immediate` if it is the same as the corresponding 50 * reference in [Dispatchers] or a short class-name representation with address otherwise. 51 */ toStringnull52 override fun toString(): String = toStringInternalImpl() ?: "$classSimpleName@$hexAddress" 53 54 /** 55 * Internal method for more specific [toString] implementations. It returns non-null 56 * string if this dispatcher is set in the platform as the main one. 57 * @suppress 58 */ 59 @InternalCoroutinesApi 60 protected fun toStringInternalImpl(): String? { 61 val main = Dispatchers.Main 62 if (this === main) return "Dispatchers.Main" 63 val immediate = 64 try { main.immediate } 65 catch (e: UnsupportedOperationException) { null } 66 if (this === immediate) return "Dispatchers.Main.immediate" 67 return null 68 } 69 } 70