我使用Alamofire从Web服务器下载五个XML文件,并使用SWXMLHash解析它们。最后一个文件依赖于前四个文件,其中某些条目引用前四个条目中包含的条目。我使用级联样式结构下载代码,以确保在下载最后一个文件之前下载所有四个文件。
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)
}
}
}
}
}
}
如您所见,每个下载段都会等待前一个下载段完成。此外,进度条在下载过程中更新。大多数XML文件从小到大小(80行--10K行),最后一个是最大的,包含大约200K行。
我不确定是否因为级联样式结构,但下载和解析前四个文件大约需要10秒钟。有可能让它更快吗?我只是想知道我是否可以提高效率。这是cpu使用情况和内存使用情况的截图。
P.S我在模拟器上运行这个应用程序。
答案 0 :(得分:1)
由于前4个下载不是相互依赖的,因此您可以同时下载它们,并且只有当收到所有4个下载时才开始第5次下载。使用调度组同步第5次下载的执行。
简而言之,整个操作如下:
ng-init