Skip to content

Instantly share code, notes, and snippets.

View af2905's full-sized avatar

Anatolii Frolov af2905

View GitHub Profile
@af2905
af2905 / lifecycle_scope_extention.txt
Last active October 22, 2023 16:57
lifecycle_scope_extention
/**
* [CoroutineScope] tied to this [LifecycleOwner]'s [Lifecycle].
*
* This scope will be cancelled when the [Lifecycle] is destroyed.
*
* This scope is bound to
* [Dispatchers.Main.immediate][kotlinx.coroutines.MainCoroutineDispatcher.immediate].
*/
public val LifecycleOwner.lifecycleScope: LifecycleCoroutineScope
get() = lifecycle.coroutineScope
@af2905
af2905 / dependencies_for_coroutines.txt
Last active October 22, 2023 16:58
dependencies_for_coroutines
dependencies {
...
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
}
@af2905
af2905 / fun_receive_data.txt
Created October 24, 2023 19:45
fun_receive_data
fun receiveData(): String {
// Long synchronous work is performed here.
return "Data received"
}
@af2905
af2905 / log_receive_data_on_ui_thread.txt
Last active October 24, 2023 19:58
log_receive_data_on_ui_thread
21:58:23.164 27359-27359 TAG Data received after 9685.0434 ms.
21:58:23.168 27359-27359 TAG onCreate completed
@af2905
af2905 / receive_data_on_ui_thread.txt
Last active October 24, 2023 19:59
receive_data_on_ui_thread
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
@OptIn(ExperimentalTime::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
@af2905
af2905 / log_receive_data_on_background_thread.txt
Created October 24, 2023 20:05
log_receive_data_on_background_thread
22:03:56.665 27504-27504 TAG onCreate completed
22:04:06.166 27504-27529 TAG Data received after 9491.5399 ms.
22:04:06.168 27504-27529 AndroidRuntime FATAL EXCEPTION: Thread-2
java.lang.NullPointerException: Can't toast on a thread that has not called Looper.prepare()
@af2905
af2905 / log_receive_data_coroutine_dispatcher_io.txt
Created October 24, 2023 20:14
log_receive_data_coroutine_dispatcher_io
22:10:09.755 27650-27650 TAG onCreate completed
22:10:18.776 27650-27675 TAG Data received after 9006.3558 ms.
22:10:18.797 27650-27675 AndroidRuntime FATAL EXCEPTION: DefaultDispatcher-worker-1
java.lang.NullPointerException: Can't toast on a thread that has not called Looper.prepare()
@af2905
af2905 / fun_receive_data_suspend.txt
Created October 24, 2023 20:25
fun_receive_data_suspend
suspend fun receiveData(): String {
withContext(Dispatchers.IO) {
// Long asynchronous work is performed here.
}
return "Data received"
}
@af2905
af2905 / log_receive_data_coroutine_dispatcher_main.txt
Created October 24, 2023 20:32
log_receive_data_coroutine_dispatcher_main
22:28:45.709 28976-28976 TAG onCreate completed
22:29:11.505 28976-28976 TAG Data received after 25069.8728 ms.