Skip to content

Commit 5f61a81

Browse files
jennantillajinliu9508
authored andcommitted
Add additional synchronized blocks to get methods
1 parent 2a3b413 commit 5f61a81

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,15 @@ open class Model(
5151
* specified, must also specify [_parentModel]
5252
*/
5353
private val _parentProperty: String? = null,
54+
private val modelSynchronizationLock: Any = Any(),
5455
) : IEventNotifier<IModelChangedHandler> {
5556
/**
5657
* A unique identifier for this model.
5758
*/
5859
var id: String
59-
get() = getStringProperty(::id.name)
60+
get() = synchronized(modelSynchronizationLock) {
61+
getStringProperty(::id.name)
62+
}
6063
set(value) {
6164
setStringProperty(::id.name, value)
6265
}
@@ -123,7 +126,8 @@ open class Model(
123126
id: String?,
124127
model: Model,
125128
) {
126-
data.clear()
129+
val newData = mutableMapOf<String, Any?>()
130+
127131
for (item in model.data) {
128132
if (item.value is Model) {
129133
val childModel = item.value as Model
@@ -138,7 +142,7 @@ open class Model(
138142
newData[::id.name] = id
139143
}
140144

141-
synchronized(initializationLock) {
145+
synchronized(modelSynchronizationLock) {
142146
data.clear()
143147
data.putAll(newData)
144148
}
@@ -665,7 +669,7 @@ open class Model(
665669
* @return The resulting [JSONObject].
666670
*/
667671
fun toJSON(): JSONObject {
668-
synchronized(initializationLock) {
672+
synchronized(modelSynchronizationLock) {
669673
val jsonObject = JSONObject()
670674
for (kvp in data) {
671675
when (val value = kvp.value) {
@@ -697,5 +701,7 @@ open class Model(
697701
override fun unsubscribe(handler: IModelChangedHandler) = changeNotifier.unsubscribe(handler)
698702

699703
override val hasSubscribers: Boolean
700-
get() = changeNotifier.hasSubscribers
704+
get() = synchronized(modelSynchronizationLock) {
705+
changeNotifier.hasSubscribers
706+
}
701707
}

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
@@ -68,7 +68,9 @@ abstract class ModelStore<TModel>(
6868
}
6969

7070
override fun get(id: String): TModel? {
71-
return models.firstOrNull { it.id == id }
71+
synchronized(models) {
72+
return models.firstOrNull { it.id == id }
73+
}
7274
}
7375

7476
override fun remove(
@@ -173,5 +175,9 @@ abstract class ModelStore<TModel>(
173175
override fun unsubscribe(handler: IModelStoreChangeHandler<TModel>) = changeSubscription.unsubscribe(handler)
174176

175177
override val hasSubscribers: Boolean
176-
get() = changeSubscription.hasSubscribers
178+
get() {
179+
synchronized(changeSubscription) {
180+
return changeSubscription.hasSubscribers
181+
}
182+
}
177183
}

0 commit comments

Comments
 (0)