Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26c09c8

Browse files
committed
added examples
1 parent 15bcad7 commit 26c09c8

31 files changed

+819
-113
lines changed

‎Tutorial1-1CoroutinesBasics/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111
targetSdkVersion rootProject.ext.targetSdkVersion
1212
versionCode 1
1313
versionName "1.0"
14-
14+
multiDexEnabled true
1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
}
1717

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter2_scopes/Activity2CoroutineScope3.kt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.widget.TextView
55
import androidx.appcompat.app.AppCompatActivity
66
import com.smarttoolfactory.tutorial1_1basics.R
77
import com.smarttoolfactory.tutorial1_1basics.databinding.Activity2Scope3Binding
8+
import com.smarttoolfactory.tutorial1_1coroutinesbasics.guide.status
89
import com.smarttoolfactory.tutorial1_1coroutinesbasics.util.dataBinding
910
import kotlinx.coroutines.*
1011

@@ -133,9 +134,9 @@ class Activity2CoroutineScope3 : AppCompatActivity(R.layout.activity2_scope_3) {
133134
it?.let {
134135

135136
runOnUiThread {
136-
binding.tvJobResult.text = it.message
137-
binding.tvChildJobResult1.text = it.message
138-
binding.tvChildJobResult2.text = it.message
137+
binding.tvJobResult.text = it.message?: currentJob?.status()
138+
binding.tvChildJobResult1.text = childJob1?.status()
139+
binding.tvChildJobResult2.text = childJob2?.status()
139140
}
140141

141142
}
@@ -175,6 +176,9 @@ class Activity2CoroutineScope3 : AppCompatActivity(R.layout.activity2_scope_3) {
175176
throw RuntimeException("Child 2 threw RuntimeException")
176177

177178
}
179+
childJob2?.invokeOnCompletion {
180+
binding.tvChildJobResult2.text = it?.message ?: childJob2?.status()
181+
}
178182

179183
withContext(Dispatchers.Main) {
180184
binding.btnCancelChildJob1.isEnabled = true

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter2_scopes/Activity2CoroutineScope4.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class Activity2CoroutineScope4 : AppCompatActivity(R.layout.activity2_scope_4) {
9696

9797
override fun onDestroy() {
9898
super.onDestroy()
99-
job.cancel()
99+
if (::job.isInitialized)
100+
job.cancel()
100101
}
101102

102103
}

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter3_lifecycle/Activity3CoroutineLifecycle.kt‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ class Activity3CoroutineLifecycle : AppCompatActivity(), CoroutineScope {
1212
private lateinit var job: Job
1313

1414
override val coroutineContext: CoroutineContext
15-
get() = job + Dispatchers.Main + CoroutineName("🙄 Activity Scope") + CoroutineExceptionHandler { coroutineContext, throwable ->
16-
println("🤬 Exception $throwable in context:$coroutineContext")
17-
}
15+
get() = job +
16+
Dispatchers.Main +
17+
CoroutineName("🙄 Activity Scope") +
18+
CoroutineExceptionHandler { coroutineContext, throwable ->
19+
println("🤬 Exception $throwable in context:$coroutineContext")
20+
}
1821

1922

2023
private val binding by lazy {
@@ -33,18 +36,18 @@ class Activity3CoroutineLifecycle : AppCompatActivity(), CoroutineScope {
3336
// 🔥⚠️ This scope lives as long as Application is alive
3437
GlobalScope.launch {
3538
for (i in 0..300) {
36-
println("🤪 Global Progress: $i in thread: ${Thread.currentThread().name}, scope: $this")
39+
println("$TAG🤪 Global Progress: $i in thread: ${Thread.currentThread().name}, scope: $this")
3740
delay(300)
3841
}
3942
}
4043

4144
// This scope is canceled whenever this Activity's onDestroy method is called
4245
launch {
4346
for (i in 0..300) {
44-
println("😍 Activity Scope Progress: $i in thread: ${Thread.currentThread().name}, scope: $this")
47+
println("$TAG😍 Activity Scope Progress: $i in thread: ${Thread.currentThread().name}, scope: $this")
4548
withContext(Dispatchers.Main) {
4649
binding.tvResult.text =
47-
"😍 Activity Scope Progress: $i in thread: ${Thread.currentThread().name}, scope: $this"
50+
"Activity Scope Progress: $i in thread: ${Thread.currentThread().name}, scope: $this"
4851
}
4952
delay(300)
5053
}
@@ -55,17 +58,11 @@ class Activity3CoroutineLifecycle : AppCompatActivity(), CoroutineScope {
5558

5659
override fun onDestroy() {
5760
super.onDestroy()
61+
// cancel() // this is the cancel method of coroutineScope. You can call coroutineScope.cancel() or parentJob.cancel() in order to cancel the parent job.
5862
job.cancel()
5963
}
6064

61-
62-
private suspend fun printNumbers(coroutineScope: CoroutineScope) {
63-
64-
for (i in 0..100) {
65-
println("Progress: $i in thread: ${Thread.currentThread().name}, scope: $coroutineScope")
66-
delay(300)
67-
}
68-
65+
companion object {
66+
const val TAG = "Test-Activity3CoroutineLifecycle"
6967
}
70-
7168
}

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter4_supervisorjob/Activity4SupervisorJob.kt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
66
import androidx.databinding.DataBindingUtil
77
import com.smarttoolfactory.tutorial1_1basics.R
88
import com.smarttoolfactory.tutorial1_1basics.databinding.Activity4SupervisorJobBinding
9+
import com.smarttoolfactory.tutorial1_1coroutinesbasics.guide.status
910
import kotlinx.coroutines.*
1011

1112

@@ -124,9 +125,9 @@ class Activity4SupervisorJob : AppCompatActivity() {
124125
it?.let {
125126

126127
runOnUiThread {
127-
binding.tvJobResult.text = it.message
128-
binding.tvChildJobResult1.text = it.message
129-
binding.tvChildJobResult2.text = it.message
128+
binding.tvJobResult.text = it.message?: currentJob?.status()
129+
binding.tvChildJobResult1.text = childJob1?.status()
130+
binding.tvChildJobResult2.text = childJob2?.status()
130131
}
131132

132133
}
@@ -164,7 +165,7 @@ class Activity4SupervisorJob : AppCompatActivity() {
164165
*/
165166

166167
val childSupervisorJob = SupervisorJob()
167-
childJob2 = myCoroutineScope.launch(childSupervisorJob) {
168+
childJob2 = myCoroutineScope.launch(childSupervisorJob) {
168169

169170
launch {
170171
displayNumbers(binding.tvChildJobResult2)
@@ -174,6 +175,9 @@ class Activity4SupervisorJob : AppCompatActivity() {
174175
throw RuntimeException("Child 2 threw RuntimeException")
175176

176177
}
178+
childJob2?.invokeOnCompletion {
179+
binding.tvChildJobResult2.text = it?.message ?: childJob2?.status()
180+
}
177181

178182
withContext(Dispatchers.Main) {
179183
binding.btnCancelChildJob1.isEnabled = true
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.smarttoolfactory.tutorial1_1coroutinesbasics.guide
2+
3+
import kotlinx.coroutines.coroutineScope
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.runBlocking
6+
7+
/*
8+
* 🔥 launch returns immediately but inside of launch not eecuted immediately. non-blocking code after launch is executed firstly, then inside of launch is executed.
9+
* 🐲 coroutineScope is a blocking code. Code execution does not go to following line,
10+
* waits for completion of inside of coroutineScope and coroutines started before coroutineScope. Thus printed 3 1 2.
11+
*
12+
* https://stackoverflow.com/questions/53535977/coroutines-runblocking-vs-coroutinescope/53536713#53536713 önce buna bak
13+
* */
14+
15+
fun main() = runBlocking {
16+
launch {
17+
println("1")
18+
}
19+
20+
coroutineScope {
21+
launch {
22+
println("2")
23+
}
24+
25+
println("3")
26+
}
27+
28+
coroutineScope {
29+
launch {
30+
println("4")
31+
}
32+
33+
println("5")
34+
}
35+
36+
launch {
37+
println("6")
38+
}
39+
40+
for (i in 7..100) {
41+
println(i.toString())
42+
}
43+
44+
println("101")
45+
}

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/guide/jobs-02.kt‎ renamed to ‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/guide/jobs-02-parentJob.cancel().kt‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ import kotlinx.coroutines.*
1616

1717
fun main() {
1818

19-
// Parent Job and Coroutine Exception Handler
2019
val parentJob = Job()
2120

22-
// CoroutineScope
2321
val coroutineScope = CoroutineScope(Dispatchers.IO + parentJob)
2422

2523
var child1: Job? = null
2624
var child2: Job? = null
2725

28-
// Use
26+
2927
coroutineScope.launch {
30-
child1 = coroutineScope.launch {
28+
child1 = launch {
3129
delay(500)
3230
}
33-
child2 = coroutineScope.launch {
31+
child2 = launch {
3432
delay(500)
3533
}
3634

@@ -40,9 +38,9 @@ fun main() {
4038

4139
println("Job 1 state: ${child1?.status()}")
4240
println("Job 2 state: ${child2?.status()}")
43-
println("Parent job is active: ${coroutineScope.isActive}")
44-
println("Parent job is active: $isActive")
45-
println("Parent job is active: ${parentJob.isActive}")
41+
println("parentJob.isActive : ${parentJob.isActive}")
42+
println("coroutineScope.isActive : ${coroutineScope.isActive}")
43+
println("coroutineScope.isActive : $isActive")
4644
}
4745

4846

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/guide/jobs-03.kt‎ renamed to ‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/guide/jobs-03-childJob.cancel().kt‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@ import kotlinx.coroutines.*
99

1010
fun main() {
1111

12-
// Parent Job and Coroutine Exception Handler
1312
val parentJob = Job()
1413

15-
// CoroutineScope
1614
val coroutineScope = CoroutineScope(Dispatchers.IO + parentJob)
1715

1816
var child1: Job? = null
1917
var child2: Job? = null
2018

2119

2220
coroutineScope.launch {
23-
child1 = coroutineScope.launch {
21+
child1 = launch {
2422
delay(500)
2523
}
26-
child2 = coroutineScope.launch {
24+
child2 = launch {
2725
delay(500)
2826
}
2927

@@ -32,9 +30,9 @@ fun main() {
3230

3331
println("Job 1 state: ${child1?.status()}")
3432
println("Job 2 state: ${child2?.status()}")
35-
println("Parent job is active: ${coroutineScope.isActive}")
36-
println("Parent job is active: $isActive")
37-
println("Parent job is active: ${parentJob.isActive}")
33+
println("parentJob.isActive : ${parentJob.isActive}")
34+
println("coroutineScope.isActive : ${coroutineScope.isActive}")
35+
println("coroutineScope.isActive : $isActive")
3836
}
3937

4038

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/guide/jobs-04.kt‎ renamed to ‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/guide/jobs-04-throw-exception-in-parent-Job.kt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@ fun main() {
2828

2929
// Use
3030
coroutineScope.launch {
31-
child1 = coroutineScope.launch {
31+
32+
child1 = launch {
3233
delay(500)
3334
}
34-
child2 = coroutineScope.launch {
35+
child2 = launch {
3536
delay(500)
3637
}
3738

38-
3939
delay(200)
4040
throw RuntimeException()
4141

42-
4342
}
4443

4544

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /