Skip to content

Commit df534d5

Browse files
committed
Add additional synchronized blocks to get methods
1 parent c742bbf commit df534d5

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling/Model.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ open class Model(
5151
* specified, must also specify [_parentModel]
5252
*/
5353
private val _parentProperty: String? = null,
54-
private val initializationLock: Any = Any(),
54+
private val modelSynchronizationLock: Any = Any(),
5555
) : IEventNotifier<IModelChangedHandler> {
5656
/**
5757
* A unique identifier for this model.
5858
*/
5959
var id: String
60-
get() = getStringProperty(::id.name)
60+
get() = synchronized(modelSynchronizationLock) {
61+
getStringProperty(::id.name)
62+
}
6163
set(value) {
6264
setStringProperty(::id.name, value)
6365
}
@@ -124,7 +126,7 @@ open class Model(
124126
id: String?,
125127
model: Model,
126128
) {
127-
val newData = Collections.synchronizedMap(mutableMapOf<String, Any?>())
129+
val newData = mutableMapOf<String, Any?>()
128130

129131
for (item in model.data) {
130132
if (item.value is Model) {
@@ -140,7 +142,7 @@ open class Model(
140142
newData[::id.name] = id
141143
}
142144

143-
synchronized(initializationLock) {
145+
synchronized(modelSynchronizationLock) {
144146
data.clear()
145147
data.putAll(newData)
146148
}
@@ -667,7 +669,7 @@ open class Model(
667669
* @return The resulting [JSONObject].
668670
*/
669671
fun toJSON(): JSONObject {
670-
synchronized(initializationLock) {
672+
synchronized(modelSynchronizationLock) {
671673
val jsonObject = JSONObject()
672674
for (kvp in data) {
673675
when (val value = kvp.value) {
@@ -699,5 +701,7 @@ open class Model(
699701
override fun unsubscribe(handler: IModelChangedHandler) = changeNotifier.unsubscribe(handler)
700702

701703
override val hasSubscribers: Boolean
702-
get() = changeNotifier.hasSubscribers
704+
get() = synchronized(modelSynchronizationLock) {
705+
changeNotifier.hasSubscribers
706+
}
703707
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling/ModelStore.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ abstract class ModelStore<TModel>(
7070
}
7171

7272
override fun get(id: String): TModel? {
73-
return models.firstOrNull { it.id == id }
73+
synchronized(models) {
74+
return models.firstOrNull { it.id == id }
75+
}
7476
}
7577

7678
override fun remove(
@@ -192,5 +194,9 @@ abstract class ModelStore<TModel>(
192194
override fun unsubscribe(handler: IModelStoreChangeHandler<TModel>) = changeSubscription.unsubscribe(handler)
193195

194196
override val hasSubscribers: Boolean
195-
get() = changeSubscription.hasSubscribers
197+
get() {
198+
synchronized(changeSubscription) {
199+
return changeSubscription.hasSubscribers
200+
}
201+
}
196202
}

0 commit comments

Comments
 (0)