我使用Alamofire库来满足我的要求。当我在模拟器上运行应用程序时它运行正常,但是当我在真实设备上运行它时会崩溃。
以下代码会导致应用崩溃:
downloadSubviewColumn(selectedMaster: selectedMaster,subviewNumber: subviewNumber){(value) -> Void in
if !value.isEmpty{
let seperatedColumn = value.components(separatedBy: "^")
var paramsString = ""
for i in seperatedColumn {
paramsString.append(";\(i)~\(self.dataArray[indexPath.row][Int(i)!])")
}
paramsString.remove(at: paramsString.startIndex)
self.downloadSubviewData(selectedMaster: self.selectedMaster , params:paramsString ,subviewNumber: self.subviewNumber){(value) -> Void in
if !value.isEmpty{
self.headerArray.removeAll()
self.dataArray.removeAll()
self.tableViewDetail.reloadData()
let arraySeperatedBySpace = value.components(separatedBy: "\r")
let arraySeperatedByPercent = arraySeperatedBySpace.filter({$0.characters.first == "%"})
self.footerSettings = arraySeperatedByPercent[0].replacingOccurrences(of: "%", with: "")
self.headerArray = self.setHeader(arraySeperatedBySpace: arraySeperatedBySpace)
self.dataArray = self.setData(arraySeperatedBySpace: arraySeperatedBySpace , headerArray: self.headerArray)
self.setTableWidth()
self.tableViewDetail.reloadData()
self.subviewNumber += 1
}else{
self.alert(message: "Veri Bulunamadı.", title: "title")
}
}
}else{
self.alert(message: "Altveri Bulunamadı.", title: "title")
}
}
以下是downloadSubviewColumn()
:
func downloadSubviewColumn(selectedMaster : String,subviewNumber : Int ,subdataCompletionHandler : @escaping CompletionHandler){
var subviewNumberString = ""
if subviewNumber < 10{
subviewNumberString = "00\(subviewNumber)"
}
DispatchQueue.global(qos: .background).async {
let url = "my_url".replacingOccurrences(of: " ", with: "%20")
Alamofire.request(url).responseString(completionHandler: { response in
switch response.result{
case .failure(_):
subdataCompletionHandler("")
SwiftSpinner.hide()
case .success(_):
guard let tempData = response.data , let detailResponse = String(data: tempData, encoding: .utf8) else {
return
}
if !detailResponse.isEmpty{
subdataCompletionHandler(detailResponse)
SwiftSpinner.hide()
}else{
subdataCompletionHandler("")
SwiftSpinner.hide()
}
}
})
// Go back to the main thread to update the UI
DispatchQueue.main.async {
}
}
}
这是downloadSubviewData()
:
func downloadSubviewData(selectedMaster : String ,params : String , subviewNumber : Int,subdataCompletionHandler : @escaping CompletionHandler){
DispatchQueue.global(qos: .background).async {
var subviewString = ""
if subviewNumber < 10{
subviewString = "00\(subviewNumber)"
}else{
subviewString = "0\(subviewNumber)"
}
let url = "my_url2".replacingOccurrences(of: " ", with: "%20")
print(url)
Alamofire.request(url).responseString(completionHandler: { response in
switch response.result{
case .failure(_):
subdataCompletionHandler("")
SwiftSpinner.hide()
case .success(_):
guard let tempData = response.data , let detailResponse = String(data: tempData, encoding: .utf8) else {
return
}
print(detailResponse)
if !detailResponse.isEmpty{
subdataCompletionHandler(detailResponse)
SwiftSpinner.hide()
}else{
subdataCompletionHandler("")
SwiftSpinner.hide()
}
}
})
// Go back to the main thread to update the UI
DispatchQueue.main.async {
}
}
}