Skip to content

Commit 4fdbb1d

Browse files
committed
move part of operationrepo initialization to a separate thread
1 parent 4c8652b commit 4fdbb1d

File tree

1 file changed

+17
-6
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl

1 file changed

+17
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ internal class OperationRepo(
6969
private val executeBucket get() =
7070
if (enqueueIntoBucket == 0) 0 else enqueueIntoBucket - 1
7171

72+
/**
73+
* Perform initialization in background to prevent possible operation point of failures from
74+
* blocking the creation of OperationRepo.
75+
*/
7276
init {
7377
val executorsMap: MutableMap<String, IOperationExecutor> = mutableMapOf()
7478
for (executor in executors) {
@@ -78,8 +82,11 @@ internal class OperationRepo(
7882
}
7983
this.executorsMap = executorsMap
8084

81-
for (operation in _operationModelStore.list()) {
82-
internalEnqueue(OperationQueueItem(operation, bucket = enqueueIntoBucket), flush = false, addToStore = false)
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+
}
8390
}
8491
}
8592

@@ -121,12 +128,16 @@ internal class OperationRepo(
121128
queueItem: OperationQueueItem,
122129
flush: Boolean,
123130
addToStore: Boolean,
131+
index: Int = -1,
124132
) {
125133
synchronized(queue) {
126-
queue.add(queueItem)
127-
if (addToStore) {
128-
_operationModelStore.add(queueItem.operation)
129-
}
134+
if (index >= 0)
135+
queue.add(index, queueItem)
136+
else
137+
queue.add(queueItem)
138+
}
139+
if (addToStore) {
140+
_operationModelStore.add(queueItem.operation)
130141
}
131142

132143
waiter.wake(LoopWaiterMessage(flush, 0))

0 commit comments

Comments
 (0)