I am making a Swift
app for iOS and I have an issue on a request I make.
This request is made every 3 to 5 seconds and the body is a 5k lines JSON (~130k characters in total) that refreshes a UITableView
. The issue is that every time .dataTask
is used on this specific request, the app freezes then runs normally after the request has been made.
By "freezes" I mean that I've detected it when I scroll my long UITableView. Even a small scroll list freezes.
I first suspected the update of the UITableView
, but I added a small 'hack' that disables the update of the UITableView
when scrolling, like this:
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("started scrolling")
self.currentlyScrolling = true
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print("stopped scrolling")
self.currentlyScrolling = false
}
It doesn't work.
My question is then: I think that dataTask is NOT run in the main thread. But I am not sure. How can I check it? Also, how can I be sure that dataTask is async ?
Here's what the dataTask
code looks like:
let task = URLSession.shared.dataTask(with: req) { data, response, err in
guard let data = data, err == nil else {
// error
return
}
if let resp = try? JSONSerialization.jsonObject(with: data) {
// success
}
}
task.resume()
Little note however, it ONLY freezes BEFORE the JSONSerialization, not after, so I guess it is not what it is causing the freeze.