我究竟做错了什么?在此先感谢您的帮助!!
我编写了以下代码,目的是控制对我的数据库以及我试图解析其数据的外部网站的请求,以便我可以填充我的数据库。目标是生成 1000 多个请求,将它们放在堆栈上,然后限制在给定时间同时触发的请求数量。它工作得很好,但每隔一段时间它就会在这行代码上生成一个异常:
RequestManager.requests?.append(_request!)
这行代码在 Push 函数 WebRequestManager 中。你可以看到下面的代码。
致命错误:UnsafeMutablePointer.destroy 计数为负
示例请求如下所示:
WebRequestManager.Request(WebRequest(_data: self.beerstores, _url: "http://brewskibooze.com/beerstore/build_database/postbeerstoreproductavailability.php"),
completion: {
(data,response,error) in
if error == nil
{
self.display(_string: "Process Complete")
}
else
{
self.display(_string: "Unable to post inventory.")
}
})
这是代码:
class WebRequest
{
private var trials = 0
private var executed = false
private var data : AnyObject?
private var url : String?
private var completion : ((data: NSData?, response: NSURLResponse?, error : ErrorType?) -> Void)?
private func execute()
{
if !executed
{
trials += 1
self.executed = true
let endpoint = NSURL(string: url!)
if (endpoint == nil) { return }
let request = NSMutableURLRequest(URL: endpoint!)
if data != nil
{
if data is NSData
{
request.HTTPMethod = "POST"
request.HTTPBody = data as? NSData
}
else
{
request.HTTPMethod = "POST"
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(data!, options: [])
}
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{
data, response, error in
if error == nil
{
WebRequestManager.pop()
self.completion?(data: data,response: response,error: error)
}
else
{
if self.trials < 10
{
self.executed = false
self.execute()
}
else
{
WebRequestManager.pop()
self.completion?(data: data,response: response,error: error)
}
}
}
task.resume()
}
}
init(_query : Query?)
{
data = _query!.data
url = _query!.toString()
}
init(_data : AnyObject?, _url : String)
{
data = _data
url = _url
}
init(_data : NSData?, _url : String)
{
data = _data
url = _url
}
deinit
{
completion = nil
data = nil
url = nil
}
}
class WebRequestManager
{
private var requests : [WebRequest]?
private static var MaxConcurrentRequests = 10
private var currentRequestCount = 0
private static var RequestManager = WebRequestManager()
private class var RequestCount : Int
{
return RequestManager.currentRequestCount
}
private class func DecrementCount()
{
RequestManager.currentRequestCount -= 1
}
private class func IncrementCount()
{
RequestManager.currentRequestCount += 1
}
private class func push(_request : WebRequest?)
{
if _request != nil
{
IncrementCount()
RequestManager.requests?.append(_request!)
}
}
private class func pop()
{
if RequestManager.requests?.count > 0
{
let last : WebRequest? = RequestManager.requests?.removeLast()
if last != nil
{
last!.execute()
DecrementCount()
}
}
}
init()
{
requests = [WebRequest]()
}
class var ActiveThreadCount : Int
{
return RequestManager.currentRequestCount
}
class func Request(_request : WebRequest?,completion : ((data: NSData?, response: NSURLResponse?, error : ErrorType?) -> Void)?)
{
_request?.completion = completion
if RequestCount < MaxConcurrentRequests
{
_request?.execute()
}
push(_request)
}
}