Ios Swift:从url下载数据会导致信号量\u等待\u陷阱冻结
在我的应用程序中,点击按钮可以从Internet站点下载数据。该站点是包含二进制数据的链接列表。有时,第一个链接可能不包含正确的数据。在本例中,应用程序获取数组中的下一个链接并从中获取数据。链接是正确的 我遇到的问题是,当我点击按钮时,应用程序经常(并非总是)冻结几秒钟。5-30秒后,解冻并正常下载机具。我明白,有东西挡住了主线。在xCode中停止进程时,我得到如下结果(注意到信号量\u wait\u陷阱): 我就是这样做的:Ios Swift:从url下载数据会导致信号量\u等待\u陷阱冻结,ios,swift,semaphore,nsoperationqueue,Ios,Swift,Semaphore,Nsoperationqueue,在我的应用程序中,点击按钮可以从Internet站点下载数据。该站点是包含二进制数据的链接列表。有时,第一个链接可能不包含正确的数据。在本例中,应用程序获取数组中的下一个链接并从中获取数据。链接是正确的 我遇到的问题是,当我点击按钮时,应用程序经常(并非总是)冻结几秒钟。5-30秒后,解冻并正常下载机具。我明白,有东西挡住了主线。在xCode中停止进程时,我得到如下结果(注意到信号量\u wait\u陷阱): 我就是这样做的: // Button Action @IBAction func d
// Button Action
@IBAction func downloadWindNoaa(_ sender: UIButton)
{
// Starts activity indicator
startActivityIndicator()
// Starts downloading and processing data
// Either use this
DispatchQueue.global(qos: .default).async
{
DispatchQueue.main.async
{
self.downloadWindsAloftData()
}
}
// Or this - no difference.
//downloadWindsAloftData()
}
}
func downloadWindsAloftData()
{
// Creates a list of website addresses to request data: CHECKED.
self.listOfLinks = makeGribWebAddress()
// Extract and save the data
saveGribFile()
}
// This downloads the data and saves it in a required format. I suspect, this is the culprit
func saveGribFile()
{
// Check if the links have been created
if (!self.listOfLinks.isEmpty)
{
/// Instance of OperationQueue
queue = OperationQueue()
// Convert array of Strings to array of URL links
let urls = self.listOfLinks.map { URL(string: $0)! }
guard self.urlIndex != urls.count else
{
NSLog("report failure")
return
}
// Current link
let url = urls[self.urlIndex]
// Increment the url index
self.urlIndex += 1
// Add operation to the queue
queue.addOperation { () -> Void in
// Variables for Request, Queue, and Error
let request = URLRequest(url: url)
let session = URLSession.shared
// Array of bytes that will hold the data
var dataReceived = [UInt8]()
// Read data
let task = session.dataTask(with: request) {(data, response, error) -> Void in
if error != nil
{
print("Request transport error")
}
else
{
let response = response as! HTTPURLResponse
let data = data!
if response.statusCode == 200
{
//Converting data to String
dataReceived = [UInt8](data)
}
else
{
print("Request server-side error")
}
}
// Main thread
OperationQueue.main.addOperation(
{
// If downloaded data is less than 2 KB in size, repeat the operation
if dataReceived.count <= 2000
{
self.saveGribFile()
}
else
{
self.setWindsAloftDataFromGrib(gribData: dataReceived)
// Reset the URL Index back to 0
self.urlIndex = 0
}
}
)
}
task.resume()
}
}
}
// Processing data further
func setWindsAloftDataFromGrib(gribData: [UInt8])
{
// Stops spinning activity indicator
stopActivityIndicator()
// Other code to process data...
}
// Makes Web Address
let GRIB_URL = "http://xxxxxxxxxx"
func makeGribWebAddress() -> [String]
{
var finalResult = [String]()
// Main address site
let address1 = "http://xxxxxxxx"
// Address part with type of data
let address2 = "file=gfs.t";
let address4 = "z.pgrb2.1p00.anl&lev_250_mb=on&lev_450_mb=on&lev_700_mb=on&var_TMP=on&var_UGRD=on&var_VGRD=on"
let leftlon = "0"
let rightlon = "359"
let toplat = "90"
let bottomlat = "-90"
// Address part with coordinates
let address5 = "&leftlon="+leftlon+"&rightlon="+rightlon+"&toplat="+toplat+"&bottomlat="+bottomlat
// Vector that includes all Grib files available for download
let listOfFiles = readWebToString()
if (!listOfFiles.isEmpty)
{
for i in 0..<listOfFiles.count
{
// Part of the link that includes the file
let address6 = "&dir=%2F"+listOfFiles[i]
// Extract time: last 2 characters
let address3 = listOfFiles[i].substring(from:listOfFiles[i].index(listOfFiles[i].endIndex, offsetBy: -2))
// Make the link
let addressFull = (address1 + address2 + address3 + address4 + address5 + address6).trimmingCharacters(in: .whitespacesAndNewlines)
finalResult.append(addressFull)
}
}
return finalResult;
}
func readWebToString() -> [String]
{
// Final array to return
var finalResult = [String]()
guard let dataURL = NSURL(string: self.GRIB_URL)
else
{
print("IGAGribReader error: No URL identified")
return []
}
do
{
// Get contents of the page
let contents = try String(contentsOf: dataURL as URL)
// Regular expression
let expression : String = ">gfs\\.\\d+<"
let range = NSRange(location: 0, length: contents.characters.count)
do
{
// Match the URL content with regex expression
let regex = try NSRegularExpression(pattern: expression, options: NSRegularExpression.Options.caseInsensitive)
let contentsNS = contents as NSString
let matches = regex.matches(in: contents, options: [], range: range)
for match in matches
{
for i in 0..<match.numberOfRanges
{
let resultingNS = contentsNS.substring(with: (match.rangeAt(i))) as String
finalResult.append(resultingNS)
}
}
// Remove "<" and ">" from the strings
if (!finalResult.isEmpty)
{
for i in 0..<finalResult.count
{
finalResult[i].remove(at: finalResult[i].startIndex)
finalResult[i].remove(at: finalResult[i].index(before: finalResult[i].endIndex))
}
}
}
catch
{
print("IGAGribReader error: No regex match")
}
}
catch
{
print("IGAGribReader error: URL content is not read")
}
return finalResult;
}
//按钮操作
@iAction func下载WindNoaa(u发件人:UIButton)
{
//启动活动指示器
startActivityIndicator()
//开始下载和处理数据
//要么用这个
DispatchQueue.global(qos:.默认值).async
{
DispatchQueue.main.async
{
self.downloadWindsAloftData()
}
}
//或者这个-没有区别。
//下载WindsaloftData()
}
}
func下载WindsaloftData()
{
//创建要请求数据的网站地址列表:选中。
self.listOfLinks=makeGribWebAddress()
//提取并保存数据
saveGribFile()
}
//这将下载数据并以所需格式保存。我怀疑,这就是罪魁祸首
func saveGribFile()
{
//检查链接是否已创建
if(!self.listOfLinks.isEmpty)
{
///OperationQueue的实例
队列=操作队列()
//将字符串数组转换为URL链接数组
让URL=self.listOfLinks.map{URL(字符串:$0)!}
guard self.urlIndex!=url.count else
{
NSLog(“报告失败”)
返回
}
//当前链接
让url=url[self.urlink]
//增加url索引
self.urlIndex+=1
//将操作添加到队列
queue.addOperation{()->中的Void
//请求、队列和错误的变量
let request=URLRequest(url:url)
让session=URLSession.shared
//将保存数据的字节数组
var dataReceived=[UInt8]()
//读取数据
让task=session.dataTask(with:request){(数据、响应、错误)->Void in
如果错误!=nil
{
打印(“请求传输错误”)
}
其他的
{
let response=响应为!HTTPURLResponse
让数据=数据!
如果response.statusCode==200
{
//将数据转换为字符串
dataReceived=[UInt8](数据)
}
其他的
{
打印(“请求服务器端错误”)
}
}
//主线
OperationQueue.main.addOperation(
{
//如果下载的数据小于2 KB,请重复该操作
如果dataReceived.count[字符串]
{
var finalResult=[String]()
//主地址站点
让地址1=”http://xxxxxxxx"
//使用数据类型寻址部件
让address2=“file=gfs.t”;
让address4=“z.pgrb2.1p00.anl&lev_250_mb=on&lev_450_mb=on&lev_700_mb=on&var_TMP=on&var_UGRD=on&var_VGRD=on”
让leftlon=“0”
让rightlon=“359”
让toplat=“90”
让bottomlat=“-90”
//用坐标表示零件
让address5=“&leftlon=“+leftlon+”&rightlon=“+rightlon+”&toplat=“+toplat+”&bottomlat=“+bottomlat
//矢量,包括所有可供下载的Grib文件
让listOfFiles=readWebToString()
如果(!listOfFiles.isEmpty)
{
对于0中的i..[字符串]
{
//要返回的最终数组
var finalResult=[String]()
guard let dataURL=NSURL(字符串:self.GRIB_URL)
其他的
{
打印(“IGAGribReader错误:未识别URL”)
返回[]
}
做
{
//获取页面的内容
let contents=try字符串(contentsOf:dataURL作为URL)
//正则表达式
let表达式:String=“>gfs\\.\\d+
您正在主线程(主队列)上调用String(contentsOf:url)
。这会将url的内容同步下载到字符串中。主线程用于驱动UI,运行同步网络代码将冻结UI
您不应该在主队列中调用readWebToString()
。执行DispatchQueue.main.async{self.downloadWindsAloftData()}
将块准确地放入主队列中,这是我们应该避免的。(async
只是意味着“稍后执行”,它仍然在Dispatch.main
上执行)
您只需在全局队列而不是主队列中运行downloadWindsAloftData
DispatchQueue.global(qos: .default).async {
self.downloadWindsAloftData()
}
当您想更新UI时,只运行DispatchQueue.main.async
。堆栈跟踪告诉您它在字符串(contentsOf:)
处停止,由readWebToString
调用,由makeGribWebAddress
调用
问题是String(contentsOf:)
执行同步网络请求。如果该请求需要任何时间,它将
DispatchQueue.global(qos: .default).async {
self.downloadWindsAloftData()
}