When ever my application fetch data from json
So you should start with the code that fetches that data. Somewhere you are probably calling [[UIApplication sharedApplication] delegate]
on a background thread. That's not allowed.
This likely means you're using your application delegate as a place to store model data. You shouldn't do that. There's almost nowhere in an app that you should reference the application delegate. (This is a very common mistake because it's sometimes done in sample code for simplicity.)
If the program crashes, the location should be in the stacktrace, so I would start by looking there, but otherwise, you'll need to audit your code (likely around JSON parsing or network requests) to find where you're doing this.
As noted in the comments below, there is no quick fix to this, and you have almost certainly made your code less stable and more likely to crash in the field. That said, creating a singleton to hold global values in Swift looks like this:
class SomeSingleton {
static let shared = SomeSingleton()
// Properties you want to be available via the singleton
}
You access it with:
SomeSingleton.shared.<property>
This does not make anything thread-safe, but if the singleton's properties are immutable (let
), then fetching them via SomeSingleton.shared
can be safely called on any thread, unlike UIApplication.shared.delegate
.
Again, it sounds like you have significant concurrency issues in this code, and this is not a quick fix; it is just one tool that is often used rather than putting random values on the AppDelegate.