Skip to content

Commit e07e987

Browse files
committed
Avoid starting sync service twice
1 parent b808c1b commit e07e987

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

demos/supabase-todolist/androidBackgroundSync/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ plugins {
33
alias(libs.plugins.kotlinAndroid)
44
alias(libs.plugins.compose.compiler)
55
id("org.jetbrains.compose")
6+
alias(libs.plugins.kotlin.atomicfu)
67
}
78

89
android {

demos/supabase-todolist/androidBackgroundSync/src/main/java/com/powersync/demo/backgroundsync/SyncService.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.powersync.PowerSyncDatabase
1414
import com.powersync.connector.supabase.SupabaseConnector
1515
import com.powersync.sync.SyncStatusData
1616
import io.github.jan.supabase.auth.status.SessionStatus
17+
import kotlinx.atomicfu.atomic
1718
import kotlinx.coroutines.CancellationException
1819
import kotlinx.coroutines.launch
1920
import org.koin.android.ext.android.inject
@@ -22,14 +23,20 @@ class SyncService: LifecycleService() {
2223

2324
private val connector: SupabaseConnector by inject()
2425
private val database: PowerSyncDatabase by inject()
26+
private var holdsServiceLock = false
2527

2628
private val notificationManager get()= NotificationManagerCompat.from(this)
2729

2830
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
2931
super.onStartCommand(intent, flags, startId)
3032

31-
createNotificationChannel()
33+
holdsServiceLock = SERVICE_RUNNING.compareAndSet(false, true)
34+
if (!holdsServiceLock) {
35+
stopSelf()
36+
return START_NOT_STICKY
37+
}
3238

39+
createNotificationChannel()
3340
ServiceCompat.startForeground(
3441
this,
3542
startId,
@@ -44,6 +51,7 @@ class SyncService: LifecycleService() {
4451
lifecycleScope.launch {
4552
database.currentStatus.asFlow().collect {
4653
try {
54+
Logger.i("Sync service received status $it")
4755
notificationManager.notify(startId, buildNotification(it))
4856
} catch (e: SecurityException) {
4957
Logger.d("Ignoring security exception when updating notification", e)
@@ -82,6 +90,14 @@ class SyncService: LifecycleService() {
8290
stopSelf(startId)
8391
}
8492

93+
override fun onDestroy() {
94+
if (holdsServiceLock) {
95+
SERVICE_RUNNING.value = false
96+
}
97+
98+
super.onDestroy()
99+
}
100+
85101
private fun createNotificationChannel() {
86102
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_LOW)
87103
.setName(getString(R.string.background_channel_name))
@@ -101,6 +117,7 @@ class SyncService: LifecycleService() {
101117
}.build()
102118

103119
private companion object {
104-
private val CHANNEL_ID = "background_sync"
120+
val CHANNEL_ID = "background_sync"
121+
val SERVICE_RUNNING = atomic(false)
105122
}
106123
}

demos/supabase-todolist/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ plugins {
66
alias(libs.plugins.kotlinMultiplatform) apply false
77
alias(libs.plugins.kotlinAndroid) apply false
88
alias(libs.plugins.cocoapods) apply false
9+
alias(libs.plugins.kotlin.atomicfu) apply false
910
}

demos/supabase-todolist/gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ android-minSdk = "24"
44
android-targetSdk = "35"
55
android-compileSdk = "35"
66
java = "17"
7+
atomicfu = "0.27.0"
78

89
# Dependencies
910
kotlin = "2.1.10"
@@ -74,5 +75,6 @@ kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref =
7475
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
7576
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
7677
buildKonfig = { id = "com.codingfeline.buildkonfig", version.ref = "buildKonfig" }
78+
kotlin-atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" }
7779

7880
[bundles]

0 commit comments

Comments
 (0)