Skip to content

Commit 0d0760d

Browse files
committed
move OperationModelStore.load() out of init block and call load after OperationRepo starts
1 parent 4fdbb1d commit 0d0760d

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl/OperationModelStore.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ import com.onesignal.user.internal.operations.impl.executors.LoginUserOperationE
2525
import com.onesignal.user.internal.operations.impl.executors.RefreshUserOperationExecutor
2626
import com.onesignal.user.internal.operations.impl.executors.SubscriptionOperationExecutor
2727
import com.onesignal.user.internal.operations.impl.executors.UpdateUserOperationExecutor
28+
import kotlinx.coroutines.Dispatchers
29+
import kotlinx.coroutines.GlobalScope
30+
import kotlinx.coroutines.coroutineScope
31+
import kotlinx.coroutines.launch
2832
import org.json.JSONObject
2933

3034
internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Operation>("operations", prefs) {
31-
init {
35+
36+
fun loadOperations() {
3237
load()
3338
}
3439

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl/OperationRepo.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ internal class OperationRepo(
8181
}
8282
}
8383
this.executorsMap = executorsMap
84-
85-
// load operation in a separate thread to avoid halting the main process
86-
coroutineScope.launch {
87-
for (operation in _operationModelStore.list().withIndex()) {
88-
internalEnqueue(OperationQueueItem(operation.value, bucket = enqueueIntoBucket), flush = false, addToStore = false, operation.index)
89-
}
90-
}
9184
}
9285

9386
override fun <T : Operation> containsInstanceOf(type: KClass<T>): Boolean {
@@ -98,7 +91,11 @@ internal class OperationRepo(
9891

9992
override fun start() {
10093
paused = false
101-
coroutineScope.launch { processQueueForever() }
94+
coroutineScope.launch {
95+
// load saved operations first then start processing the queue to ensure correct operation order
96+
loadSavedOperations()
97+
processQueueForever()
98+
}
10299
}
103100

104101
override fun enqueue(
@@ -128,10 +125,10 @@ internal class OperationRepo(
128125
queueItem: OperationQueueItem,
129126
flush: Boolean,
130127
addToStore: Boolean,
131-
index: Int = -1,
128+
index: Int? = null,
132129
) {
133130
synchronized(queue) {
134-
if (index >= 0)
131+
if (index != null)
135132
queue.add(index, queueItem)
136133
else
137134
queue.add(queueItem)
@@ -363,4 +360,16 @@ internal class OperationRepo(
363360

364361
return ops
365362
}
363+
364+
/**
365+
* Load saved operations from preference service and add them into the queue
366+
* NOTE: sometimes the loading might take longer than expectedly due to device's state
367+
*/
368+
private fun loadSavedOperations() {
369+
// load operation in a separate thread to avoid halting the main process
370+
_operationModelStore.loadOperations()
371+
for (operation in _operationModelStore.list().withIndex()) {
372+
internalEnqueue(OperationQueueItem(operation.value, bucket = enqueueIntoBucket), flush = false, addToStore = false, operation.index)
373+
}
374+
}
366375
}

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/operations/OperationRepoTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private class Mocks {
3535
val operationModelStore: OperationModelStore =
3636
run {
3737
val mockOperationModelStore = mockk<OperationModelStore>()
38+
every {mockOperationModelStore.loadOperations() } just runs
3839
every { mockOperationModelStore.list() } returns listOf()
3940
every { mockOperationModelStore.add(any()) } just runs
4041
every { mockOperationModelStore.remove(any()) } just runs

0 commit comments

Comments
 (0)