JSON Decoding on Main Thread

The main (or UI) thread in a mobile app is responsible for handling all user interaction and needs to be able to respond to gestures and taps in real time. If a long-running operation blocks the main thread, the app becomes unresponsive, impacting the quality of the user experience.

The process of decoding a JSON blob into a data structure, is an example of a long-running operation that may impact app responsiveness. If a long-running JSON decoding operation is detected on the main thread, Sentry will flag it as an issue.

Decoding a JSON blob into a data structure can be a long running operation, especially for large blobs. This issue type is created when we detect a long running JSON decoding operation on the main thread.

Detection Criteria

Profiling must be enabled for Sentry to detect JSON Decoding on Main Thread issues. Sentry looks for functions related to JSON decoding that have been running on the main thread for at least 40ms. This threshold ensures the function appears in a sufficient number of samples collected by the profile (~10ms per sample x 4 = ~40ms total) before it gets detected as an issue.

Function Evidence

To find additional information about your JSON Decoding on Main Thread problem, go to its Issue Details page and scroll down to the "Function Evidence" section, which shows the following:

  • Transaction Name: The name of the transaction where the issue was detected.
  • Suspect function: The function that triggered the issue detection (in this case, a JSON decoding function).
  • Duration: How long the function took to execute and the number of consecutive samples collected by the profiler that contained the function.

JSON Decoding on Main Thread Function Evidence

To view the entire profile associated with the issue, click the “View Profile” button.

The profile will indicate where the suspect function was called from, along with other functions being called by the suspect function:

JSON Decoding on Main Thread Profile

Stack Trace

The “Stack Trace” section shows a full stack trace for where the detected JSON decoding function was called from:

Image Decoding on Main Thread Stack Trace



The following code updates a view by calling JSONDecoder.decode<T> to decode JSON on the main thread:

func updatePersonView(json: Data) throws {
  personView.person = try JSONDecoder().decode(Person.self, from: json)

Performance could be improved by moving the JSON decoding to a background queue:

func updatePersonView(json: Data) {
	DispatchQueue.global(qos: .userInitiated).async {
    do {
			let person = try JSONDecoder().decode(Person.self, from: json)
			DispatchQueue.main.async {
				personView.person = person
    catch let error {
			print(error) // handle error


The following code updates a view by calling Gson.fromJson()to decode JSON on the UI thread:

fun updatePersonView(json: String) {
	personView.person = Gson().fromJson(json, Person::class.java)

Performance could be improved by moving the JSON decoding to a coroutine:

fun updatePersonView(json: String) {
	lifecycleScope.launch(Dispatchers.IO) {
	  val person = Gson().fromJson(json, Person::class.java)

    withContext(Dispatchers.Main) {
      personView.person = person
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
Suggest an edit to this page   |  Contribute to Docs   |  

Report a Problem

🎉 Thank you for your feedback! 🙌
Report a problem