Skip to content

Commit bb25e15

Browse files
committed
Avoid keeping the synchronized list locked for longer than necessary
1 parent 0d0760d commit bb25e15

File tree

1 file changed

+19
-16
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling

1 file changed

+19
-16
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,34 @@ abstract class ModelStore<TModel>(
154154
}
155155

156156
protected fun load() {
157+
if (name == null || _prefs == null)
158+
return
159+
160+
val str = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, "[]")
161+
val jsonArray = JSONArray(str)
157162
synchronized(models) {
158-
if (name != null && _prefs != null) {
159-
val str = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, "[]")
160-
val jsonArray = JSONArray(str)
161-
for (index in 0 until jsonArray.length()) {
162-
val newModel = create(jsonArray.getJSONObject(index)) ?: continue
163-
models.add(newModel)
164-
// listen for changes to this model
165-
newModel.subscribe(this)
166-
}
163+
for (index in 0 until jsonArray.length()) {
164+
val newModel = create(jsonArray.getJSONObject(index)) ?: continue
165+
models.add(newModel)
166+
// listen for changes to this model
167+
newModel.subscribe(this)
167168
}
168169
}
170+
169171
}
170172

171173
fun persist() {
172-
synchronized(models) {
173-
if (name != null && _prefs != null) {
174-
val jsonArray = JSONArray()
175-
for (model in models) {
176-
jsonArray.put(model.toJSON())
177-
}
174+
if (name == null || _prefs == null)
175+
return
178176

179-
_prefs.saveString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, jsonArray.toString())
177+
val jsonArray = JSONArray()
178+
synchronized(models) {
179+
for (model in models) {
180+
jsonArray.put(model.toJSON())
180181
}
181182
}
183+
184+
_prefs.saveString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, jsonArray.toString())
182185
}
183186

184187
override fun subscribe(handler: IModelStoreChangeHandler<TModel>) = changeSubscription.subscribe(handler)

0 commit comments

Comments
 (0)