DispatchQueue.main.sync返回exc_bad_instruction Swift 3 [英] DispatchQueue.main.sync returning exc_bad_instruction Swift 3

查看:111
本文介绍了DispatchQueue.main.sync返回exc_bad_instruction Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中显示一个ActivityIndi​​catorView,但是当我从主线程调用sync方法时,该应用程序崩溃并显示错误:exc_bad_instruction (code=exc_i386_invop subcode=0x0) 我正在使用xcode 8.0和swift 3

I want to display an ActivityIndicatorView in my app, but when I call the sync method from the main thread, the app crashes with the error: exc_bad_instruction (code=exc_i386_invop subcode=0x0) I'm using xcode 8.0 and 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
}

推荐答案

此处要执行的操作是在退出后台线程之前,从后台线程同步启动主线程.这是一个逻辑错误.

What you are trying to do here is to launch the main thread synchronously from a background thread before it exits. This is a logical error.

您应该这样做:

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

这篇关于DispatchQueue.main.sync返回exc_bad_instruction Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆