DispatchQueue.main.sync 返回 exc_bad_instruction Swift 3

问题描述 投票:0回答:4

我想在我的应用程序中显示 ActivityIndicatorView,但是当我从主线程调用

sync
方法时,应用程序崩溃并出现错误:
exc_bad_instruction (code=exc_i386_invop subcode=0x0)
我正在使用 xcode 8.0 和 swift 3

有人可以帮助我吗?

 func POST(endpoint:NSString!,body:NSString!,vc:UIViewController? = nil)->NetworkResult{
    let result = NetworkResult()
    DispatchQueue.main.sync {
        self.displayActivityIndicator(viewController: vc)
    }
    let urlStr = self.url.appending(endpoint as String).appending(self.getHashAutenticacao() as String)
    print(urlStr)
    let request = NSMutableURLRequest(url: URL(string: urlStr)!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 20)
    print(request.debugDescription)
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "POST"
    request.httpBody = body.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: true)

    // send the request
    var data: NSData!
    do {
        data = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &self.response) as NSData!
    } catch let error1 as NSError {
        self.error = error1
        data = nil
    }
    if let httpResponse = self.response as? HTTPURLResponse {
        result.resultCode = httpResponse.statusCode

        if httpResponse.statusCode == 200{
            if data != nil{
                if data.length > 0{
                    let json = (try! JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers))
                    if let jsonArray:NSArray = json as? NSArray{
                        result.data = jsonArray
                    }else{
                        if let jsonDict:NSDictionary = json as? NSDictionary{
                            result.data = [jsonDict]
                        }
                    }
                }
            }
        }
    }else {
        result.message = self.error!.debugDescription as NSString?
    }

    DispatchQueue.main.sync {
        self.hideActivityIndicator(viewController: vc)
    }
    return result
}
ios swift swift3 grand-central-dispatch
4个回答
56
投票

您在这里尝试做的是在退出之前从后台线程同步启动主线程。这是一个逻辑错误。

你应该这样做:

DispatchQueue.global().async(execute: {
    print("teste")
    DispatchQueue.main.sync{
        print("main thread")
    }
})

46
投票

也许是因为您正在尝试从主线程 DispatchQueue.main.sync 。您可以检查您是否已经在主线程中,例如:

if Thread.isMainThread {
  // do stuff
} else {
  DispatchQueue.main.sync {
    // do stuff
  }
}

4
投票

如果你想从主线程读取值,这里有一个方便的函数:

func syncMain<T>(_ closure: () -> T) -> T {
    if Thread.isMainThread {
        return closure()
    } else {
        return DispatchQueue.main.sync(execute: closure)
    }
}

用途:

// we are in BG or Main, does not matter
let value = syncMain {
    // we are in Main for sure
    return ...
}

0
投票

根据下面给出的 Apple 文档。 https://developer.apple.com/documentation/dispatch/dispatchqueue/1452870-sync

在当前目标队列(这里是主线程)中调用sync函数可能会导致死锁状态。因此,将会引发错误。所以你必须改变线程或者你必须异步调用。

建议: 您可以使用全局线程来执行该函数。

DispatchQueue.global().sync {
    self.displayActivityIndicator(viewController: vc)
}

或者您也可以在主线程中使用异步函数。

DispatchQueue.main.async {
    self.displayActivityIndicator(viewController: vc)
}
© www.soinside.com 2019 - 2024. All rights reserved.