I am parsing json in a function call. Most of the time the function gets executed but after checking my app crash report it is observed that it crashes at particular line sometimes. It is a rare crash but I am uncertain what makes it behave like that. Being amateur developer, I cannot get to exact solution, check out if you can?
func doThis() { //url let url = URL(string: “URL”) var request = URLRequest(url: url!) //httpmethod request.httpMethod = "GET" //session let session = URLSession.shared //task let task = session.dataTask(with: request, completionHandler: {(data: Data?, response: URLResponse?,error: Error?) -> Void in if error != nil { print("unable to fetch") } do { //crashes on below line let rootDic = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary //crashes at above line. -JsonParsing- } catch{ print("error") } } ) //resumetask task.resume() }
Is it because of mobile network issues or low speed of internet on user side? Or threading mistake by me?