0

Following is my code

@NonNull
private T readLocalResource() {
 synchronized (this) {
        if (data == null) {
            String resourceAsString = sharedPreferences.getString(key, null);

            if (TextUtils.isEmpty(resourceAsString)){
                data = initialResource;
            }
            else {
                data = parseJson(resourceAsString);
            }
        }

        return data;
    }
 }

Above code is causing ANR in few cases following is the ANR log

"main" tid=1 Blocked
at com.mypackage.ResourceCache.readLocalResource (MyResourceCache.java:101)
at com.mypackage.ResourceCache.get (MyResourceCache.java:73)
at com.mypackage.datacollector.DataLayer.lambda$createRegistry$5 (DataLayer.java:73)

How to fix this?

amodkanthe
  • 4,345
  • 6
  • 36
  • 77
  • What's on line 101? And it's possible that your `parseJson` is blocking the main thread because the string might be too long. Consider using an async. approach like `Coroutines` or `ExecutorService`. – Darshan Apr 24 '23 at 15:36
  • synchronized (this) this line is 101 – amodkanthe Apr 24 '23 at 15:39

0 Answers0