3

我究竟做错了什么?在此先感谢您的帮助!!

我编写了以下代码,目的是控制对我的数据库以及我试图解析其数据的外部网站的请求,以便我可以填充我的数据库。目标是生成 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)
}

}

4

2 回答 2

0

我很确定,我只是回答了我自己的问题。问题在于,在线程环境中,由于所有这些大量请求以及请求的多次弹出和推送,存在同时弹出和推送的风险。解决方案似乎是这样的:

objc_sync_enter(锁定)

objc_sync_exit(锁定)

此处到期的信用:

快速创建线程安全数组

但是,这会导致这个警告......

|同步跳过| 在快照块内启动了栅栏 - 跳过工作区同步,因为它可能会从栅栏ExemptQueue 中取出消息,并且快照预计不会发生 2016-05-14 14:11:57.791 _____ [348:99565] |synchronize-skip| 在快照块内启动栅栏 - 跳过工作区同步,因为它可能会从栅栏ExemptQueue 中取出消息,并且快照预计不会发生这种情况

于 2016-05-14T18:08:08.617 回答
0

您为我指明了正确的方向,串行调度队列在没有警告的情况下解决了我的问题。

在 swift 3 中创建一个串行调度队列:

    let serialQueue = DispatchQueue(label: "myqueue")`

然后修改队列中的数组:

    serialQueue.sync {
        //modify array
    }
于 2017-02-14T15:23:47.340 回答