Skip to content

Commit

Permalink
fix ANR caused by operationRepo.enqueue while loading is not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
jinliu9508 committed Dec 10, 2024
1 parent 11b3564 commit fecb45d
Showing 1 changed file with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,33 +169,34 @@ abstract class ModelStore<TModel>(

val str = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, "[]")
val jsonArray = JSONArray(str)
synchronized(models) {
val shouldRePersist = models.isNotEmpty()
for (index in jsonArray.length() - 1 downTo 0) {
val newModel = create(jsonArray.getJSONObject(index)) ?: continue

/*
* NOTE: Migration fix for bug introduced in 5.1.12
* The following check is intended for the operation model store.
* When the call to this method moved out of the operation model store's initializer,
* duplicate operations could be cached.
* See https://github.com/OneSignal/OneSignal-Android-SDK/pull/2099
*/
val hasExisting = models.any { it.id == newModel.id }
if (hasExisting) {
Logging.debug("ModelStore<$name>: load - operation.id: ${newModel.id} already exists in the store.")
continue
}

models.add(0, newModel)
// listen for changes to this model
newModel.subscribe(this)
val shouldRePersist = models.isNotEmpty()
for (index in jsonArray.length() - 1 downTo 0) {
val newModel = create(jsonArray.getJSONObject(index)) ?: continue

/*
* NOTE: Migration fix for bug introduced in 5.1.12
* The following check is intended for the operation model store.
* When the call to this method moved out of the operation model store's initializer,
* duplicate operations could be cached.
* See https://github.com/OneSignal/OneSignal-Android-SDK/pull/2099
*/
val hasExisting = models.any { it.id == newModel.id }
if (hasExisting) {
Logging.debug("ModelStore<$name>: load - operation.id: ${newModel.id} already exists in the store.")
continue
}
hasLoadedFromCache = true
// optimization only: to avoid unnecessary writes
if (shouldRePersist) {
persist()

synchronized(models) {
models.add(0, newModel)
}
// listen for changes to this model
newModel.subscribe(this)
}
hasLoadedFromCache = true
// optimization only: to avoid unnecessary writes
if (shouldRePersist) {
persist()
}
}

Expand Down

0 comments on commit fecb45d

Please sign in to comment.