为什么我收到UnsafeMutablePointer错误?

时间:2016-05-14 15:35:18

标签: ios swift unsafemutablepointer

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

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

}

2 个答案:

答案 0 :(得分:0)

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

objc_sync_enter(锁)

objc_sync_exit(锁)

此处的到期信用:

Create thread safe array in swift

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

| synchronize-skip | 在快照块内部启动了一个防火墙 - 跳过工作区同步,因为它可能会使来自fenceExemptQueue的消息出局,而快照期望不会发生 2016-05-14 14:11:57.791 _____ [348:99565] | synchronize-skip | 在快照块内部启动了栅栏 - 跳过工作区同步,因为它可能会使来自fenceExemptQueue的消息出列并且快照期望不会发生

答案 1 :(得分:0)

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

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

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

然后修改队列中的数组:

    serialQueue.sync {
        //modify array
    }