I have function call in viewDidLoad method just like that
override func viewDidLoad(){
super.viewDidLoad()
getSubCategories()
}
And the func definition is below
func getSubCategories(){
let parameters = xx
let url = URL(string: "xx")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
let task = session.dataTask(with: request as URLRequest, completionHandler: { [self] data, response, error in
do {
if (try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]) != nil {
The function executes until here then jumps to the task.resume skipping the code after that
subCategoryModel = try JSONDecoder().decode(SubCategoryModel.self, from: data)
print(subCategoryModel!)
}
} catch let error {
print(error)
}
})
task.resume()
before decoding the data the collectionViews start reloading and show the error that values are nil. I have set the delegate and datasource in viewdidload. How do I write the code that collectionsviews are loaded after the decoding process.