{"record":{"id":"e70170e56589d4d1","repo":"bumptech/glide","slug":"you-must-call-this-method-on-a-background-thread","errorCode":null,"errorMessage":"You must call this method on a background thread","messagePattern":"You must call this method on a background thread","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/bumptech/glide/util/Util.java","lineNumber":192,"sourceCode":"      }\n    }\n    return mainThreadHandler;\n  }\n\n  /**\n   * Throws an {@link java.lang.IllegalArgumentException} if called on a thread other than the main\n   * thread.\n   */\n  public static void assertMainThread() {\n    if (!isOnMainThread()) {\n      throw new IllegalArgumentException(\"You must call this method on the main thread\");\n    }\n  }\n\n  /** Throws an {@link java.lang.IllegalArgumentException} if called on the main thread. */\n  public static void assertBackgroundThread() {\n    if (!isOnBackgroundThread()) {\n      throw new IllegalArgumentException(\"You must call this method on a background thread\");\n    }\n  }\n\n  /** Returns {@code true} if called on the main thread, {@code false} otherwise. */\n  public static boolean isOnMainThread() {\n    return Looper.myLooper() == Looper.getMainLooper();\n  }\n\n  /** Returns {@code true} if called on a background thread, {@code false} otherwise. */\n  public static boolean isOnBackgroundThread() {\n    return !isOnMainThread();\n  }\n\n  /** Creates a {@link java.util.Queue} of the given size using Glide's preferred implementation. */\n  @NonNull\n  public static <T> Queue<T> createQueue(int size) {\n    return new ArrayDeque<>(size);\n  }","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/bumptech/glide/blob/eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a/library/src/main/java/com/bumptech/glide/util/Util.java#L174-L210","documentation":"Thrown by Util.assertBackgroundThread() when the calling code is on the main (UI) thread. Some Glide internals and APIs (notably certain cache/disk operations and RequestBuilder.asDrawable().submit() style synchronous fetches run on background executors) require a background thread to avoid blocking the UI; calling them on the main thread is rejected.","triggerScenarios":"Calling a Glide API marked as background-only from the UI thread directly; e.g. certain disk-cache methods, or synchronous submit().get() patterns; helper methods inside custom decoders/modules that assert background execution.","commonSituations":"Calling Glide's disk cache inspection APIs (e.g. Glide.get(ctx).clearDiskCache()) from an onClick handler; synchronous fetch via RequestBuilder.submit().get() on the main thread; custom logic invoking assertBackgroundThread-bearing utilities during bind.","solutions":["Move the call off the main thread: withContext(Dispatchers.IO) { ... } or an AsyncTask/Executor","For clearDiskCache specifically, run it on a background executor and post UI updates back","For fetching an image synchronously, use submit().get() only on a background thread, or switch to an async into() with a Target","Audit the stack trace to find the UI-thread caller of the background-only method"],"exampleFix":"// before\nfun onClearCacheClicked() {\n  Glide.get(this).clearDiskCache() // throws: main thread\n}\n\n// after\nfun onClearCacheClicked() {\n  lifecycleScope.launch(Dispatchers.IO) {\n    Glide.get(this@Activity).clearDiskCache()\n  }\n}","handlingStrategy":"validation","validationCode":"fun isBackgroundThread(): Boolean =\n  Looper.myLooper() != Looper.getMainLooper()\n\nif (isBackgroundThread()) {\n  Glide.get(ctx).clearDiskCache()\n} else {\n  // move to a background executor\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run clearDiskCache() and other background-only APIs on Dispatchers.IO / an Executor","Never call submit().get() synchronous fetches on the main thread","Audit onClick handlers for direct background-only Glide calls"],"tags":["android","glide","threading","background-thread","concurrency"],"backgroundTag":null,"analyzedSha":"eb14a895d8f866e6a08c3e19d50ea3bd2c12c20a","analyzedAt":"2026-08-14T01:43:54.821Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}