I am trying to download data from a school course catalog site. I have 64 Urls in the variable UrlBook. I have successfully written code to download a collection of courses and turn them into a single subject object from a single url using completion handler method. I don't really know how should I implement to collect all the subjects from 64 url and eventually turn them into a catalog object (it contains a list of subject objects).
I have read many articles and posts on asynchronous and synchronous processing, it's just so confusing to me. I would really appreciate easy and straight forward code to help me solve this problem. Thank you guys!
let urlBook = getUrlFromBook()
func fetchClassInfo(url:URL,completion: @escaping ([clase])-> Void){
let task = URLSession.shared.dataTask(with: url){(data, response, error) in
let jsonDecoder = JSONDecoder()
if let data = data,
let collection:[clase] = try? jsonDecoder.decode([clase].self, from: data){
completion(collection)
}else{
print("Either no data was returned, or data was not properly decoded.")
//completion(nil)
}
}
task.resume()
}
fetchClassInfo(url:urlBook.book[0]){(clase) in
let newSubject = makeNewSubject(subjectIndex: 0, collectionOfCourse: clase)
var masterCatalog = catalog(subjectCollection: [])
masterCatalog.addSubject(newSubject: newSubject)
}