# JSON Decoding on Main Thread

## Learn more about JSON Decoding on Main Thread issues and how to diagnose and fix them.

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](#detection-criteria)

[Profiling](/product/explore/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](#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](/_next/image/?url=%2Fmdx-images%2Fjson-decoding-function-evidence-REL2NTD5.png%231586x698\&w=3840\&q=75)](/mdx-images/json-decoding-function-evidence-REL2NTD5.png)

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](/_next/image/?url=%2Fmdx-images%2Fjson-decoding-profile-D44SSVDW.png%231694x392\&w=3840\&q=75)](/mdx-images/json-decoding-profile-D44SSVDW.png)

## [Stack Trace](#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](/_next/image/?url=%2Fmdx-images%2Fjson-decoding-stack-trace-I5YHQOQN.png%231594x430\&w=3840\&q=75)](/mdx-images/json-decoding-stack-trace-I5YHQOQN.png)

## [Example](#example)

### [iOS](#ios)

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

```swift
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:

```swift
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
    }
	}
}
```

### [Android](#android)

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

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

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

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

    withContext(Dispatchers.Main) {
      personView.person = person
    }
  }
}
```
