After getting JSON data from Alamofire request, I want to parse it on background thread, because, parsing process is difficult and has some logic in my case. The first idea to solve was executing a async operation on background thread with synchronous request with Alamofire. But, as I've read, Alamofire works completely asynchronously. The second solution of mine was executing a parsing JSON process in another thread after responseJSON of Alamofire gets called. After it completes, I will update my UI in main thread. Here is how it looks:
.responseJSON { (response) in //callback of Alamofire if response.result.isSuccess { let json = JSON(response.result.value!) DispatchQueue(label: "parsing", qos: .userInitiated).async { self.parseJSON(json) DispatchQueue.main.async { self.updateUI() } } } }
But is it a good approach to solve this problem? I just don't want to parse a JSON on main thread, because parsing process has a couple of for loops and other operations, and, I think it cause on drawing of UI related stuff.
#ios #json #swift