gpt4 book ai didi

ios - 为什么我会收到 UnsafeMutablePointer 错误?

转载 作者:可可西里 更新时间:2023-10-31 23:43:47 26 4
gpt4 key购买 nike

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

我编写了以下代码,目的是控制对我的数据库的请求,以及对我试图解析其数据的外部网站的请求,以便我可以填充我的数据库。目标是生成 1000 多个请求,将它们放在堆栈上,然后限制在给定时间同时触发的请求数。它工作得很好,但有时它会在这行代码上产生异常:

RequestManager.requests?.append(_request!)

这行代码在WebRequestManager的Push函数中。你可以看到下面的代码。

fatal error :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)
}

最佳答案

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

objc_sync_enter(锁)

objc_sync_exit(锁)

此处应归功于:

Create thread safe array in swift

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

|synchronize-skip| 在快照 block 内启动了一个栅栏 - 跳过工作区同步,因为它可能会使来自 fenceExemptQueue 的消息出列,而快照预计不会发生这种情况2016-05-14 14:11:57.791 _____ [348:99565] |synchronize-skip| 在快照 block 内启动了栅栏 - 跳过工作区同步,因为它可能使来自 fenceExemptQueue 的消息出列并且快照预计这不会发生

关于ios - 为什么我会收到 UnsafeMutablePointer 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com