Skip to content

Commit 61dc5aa

Browse files
authored
sync: smoother download service lifecycle scoping (fixes #9030) (#9008)
1 parent 44290f1 commit 61dc5aa

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "org.ole.planet.myplanet"
1010
minSdk = 26
1111
targetSdk = 36
12-
versionCode = 3717
13-
versionName = "0.37.17"
12+
versionCode = 3718
13+
versionName = "0.37.18"
1414
ndkVersion = '26.3.11579264'
1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
vectorDrawables.useSupportLibrary = true

app/src/main/java/org/ole/planet/myplanet/datamanager/MyDownloadService.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import java.io.IOException
2424
import kotlin.math.roundToInt
2525
import kotlinx.coroutines.CoroutineScope
2626
import kotlinx.coroutines.Dispatchers
27+
import kotlinx.coroutines.SupervisorJob
2728
import kotlinx.coroutines.launch
2829
import okhttp3.ResponseBody
2930
import org.ole.planet.myplanet.MainApplication.Companion.createLog
@@ -50,6 +51,9 @@ class MyDownloadService : Service() {
5051
private var totalDownloadsCount = 0
5152
private var completedDownloadsCount = 0
5253

54+
private val downloadJob = SupervisorJob()
55+
private val downloadScope = CoroutineScope(downloadJob + Dispatchers.IO)
56+
5357
override fun onBind(intent: Intent?): IBinder? = null
5458

5559
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -75,7 +79,7 @@ class MyDownloadService : Service() {
7579

7680
updateNotificationForBatchDownload()
7781

78-
CoroutineScope(Dispatchers.IO).launch {
82+
downloadScope.launch {
7983
urls.forEachIndexed { index, url ->
8084
currentIndex = index
8185
initDownload(url, fromSync)
@@ -343,6 +347,7 @@ class MyDownloadService : Service() {
343347
stopForeground(true)
344348
} catch (_: Exception) {
345349
}
350+
downloadJob.cancel()
346351
notificationManager?.cancel(ONGOING_NOTIFICATION_ID)
347352
super.onDestroy()
348353
}

0 commit comments

Comments
 (0)