I'm downloading five XML files from the web server using Alamofire and parsing them using SWXMLHash. The last file depends on the first four files in a way that some of its entries refer to the entries contain in the first four. I use cascading style structure for download code to ensure all four files are downloaded before downloading of the last file can start.
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)[0]
let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
let fileURL = documentsURL.appendingPathComponent("image.png")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
// Download Category files
Alamofire.download(self.ottawaOpenDataCategoriesURL, to:
destination)
.downloadProgress { progress in
//print("Download Progress for Category: \(progress.fractionCompleted)")
if let pv = self.progressViewController {
pv.updateProgress(progress.fractionCompleted * 100.0) //Alamofire returns 1 for complete process
}
}
.responseData { response in
// Check 304 response to see if new file is available
if response.response?.statusCode == self.HTTP_STATUS_FILE_NOT_CHANGE {
return
}
if let data = response.result.value {
let xml = self.initXMLParser(data: data)
self.storeCategoryXMLStream(xml)
}
// After complete downloading or get error, try download Options
Alamofire.download(self.ottawaOpenDataOptionsURL, to:
destination)
.downloadProgress { progress in
if let pv = self.progressViewController {
pv.updateProgress(progress.fractionCompleted * 100.0) //Alamofire returns 1 for complete process
}
}
.responseData { response in
// Check 304 response to see if new file is available
if response.response?.statusCode == self.HTTP_STATUS_FILE_NOT_CHANGE {
return
}
if let data = response.result.value {
let xml = self.initXMLParser(data: data)
self.storeEventOptionsXMLStream(xml)
}
// After complete downloading or get error, try download Locations
Alamofire.download(self.ottawaOpenDataLocationsURL, to:
destination)
.downloadProgress { progress in
if let pv = self.progressViewController {
pv.updateProgress(progress.fractionCompleted * 100.0) //Alamofire returns 1 for complete process
}
}
.responseData { response in
// Check 304 response to see if new file is available
if response.response?.statusCode == self.HTTP_STATUS_FILE_NOT_CHANGE {
return
}
if let data = response.result.value {
let xml = self.initXMLParser(data: data)
self.storeVenuesXMLStream(xml)
}
// After complete downloading or get error, try download CitrSectors
Alamofire.download(self.ottawaOpenDataCitySectorsURL, to:
destination)
.downloadProgress { progress in
if let pv = self.progressViewController {
pv.updateProgress(progress.fractionCompleted * 100.0) //Alamofire returns 1 for complete process
}
}
.responseData { response in
// Check 304 response to see if new file is available
if response.response?.statusCode == self.HTTP_STATUS_FILE_NOT_CHANGE {
return
}
if let data = response.result.value {
let xml = self.initXMLParser(data: data)
self.storeCitySectorsXMLStream(xml)
}
// After complete downloading or get error, try download events
Alamofire.download(self.ottawaOpenDataEventsURL, to:
destination)
.downloadProgress { progress in
if let pv = self.progressViewController {
pv.updateProgress(progress.fractionCompleted * 100.0) //Alamofire returns 1 for complete process
}
}
.responseData { response in
// Check 304 response to see if new file is available
if response.response?.statusCode == self.HTTP_STATUS_FILE_NOT_CHANGE {
return
}
if let data = response.result.value {
let xml = self.initXMLParser(data: data)
self.storeEventsXMLStream(xml)
}
}
}
}
}
}
As you can see, each download segment waits for the previous one to complete. Also, the progress bar is updated during download process. Most XML files are from small to decent size (80 Lines - 10K lines) and the last one is the largest and contains about 200K lines.
I'm not sure if it's because of cascading style structure, but it takes about 10 seconds to download and parse the first four files. Is it possible to make it faster? I just want to know if I can improve efficiency. Here's a screenshot of cpu usage and mem usage.
P.S I'm running this app on simulator.