3

What am I doing wrong? Thanks in advance for your help!!

I wrote the following code with the goal of controlling requests to my database, and also to an external website that I was trying to parse for its data so I could fill my database. The goal was to generate 1000+ requests, put them on a stack, and then limit how many simultaneous requests were fired at a given time. It works very well, but every so often it generates an exception on this line of code:

RequestManager.requests?.append(_request!)

This line of code is in the Push function WebRequestManager. You can see the code below.

fatal error: UnsafeMutablePointer.destroy with negative count

An example request looks like this:

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.")
                                        }


                                    })

Here's the code:

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)
}

}

bhardy
  • 83
  • 10
  • Which line did the Debuger stop at? And your code is so long... – Lumialxk May 14 '16 at 15:39
  • Sorry, about the long code. I was trying to be thorough with my description of the problem. It quits in the push() function of WebRequestManager RequestManager.requests?.append(_request!) – bhardy May 14 '16 at 15:46
  • Is it possible that with multiple requests involving pushes and pops that I would find myself in a position where it is simultaneously popping and pushing? How would I work around that problem? – bhardy May 14 '16 at 17:35

2 Answers2

0

I am pretty sure, I just answered my own question. The issue is that with all of these many requests and many pops and pushes of requests in a threaded environment, there is a risk of a simultaneous pop and push. The solution appears to be this:

objc_sync_enter(lock)

objc_sync_exit(lock)

Credit due here:

Create thread safe array in swift

But, that causes this warning ...

|synchronize-skip| a fence was started inside of a snapshot block - skipping the workspace synchronize because it may dequeue messages from the fenceExemptQueue and snapshots expect that not to happen 2016-05-14 14:11:57.791 _____ [348:99565] |synchronize-skip| a fence was started inside of a snapshot block - skipping the workspace synchronize because it may dequeue messages from the fenceExemptQueue and snapshots expect that not to happen

Community
  • 1
  • 1
bhardy
  • 83
  • 10
0

You pointed me in the right direction, the serial dispatch queue resolved my issue without warnings.

Create a serial dispatch queue in swift 3:

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

Then modify the array in the queue:

    serialQueue.sync {
        //modify array
    }
Stephanus Mostert
  • 909
  • 10
  • 11