ios - example - DispatchQueue.main.sync que devuelve exc_bad_instruction Swift 3
nsoperationqueue swift 3 example (1)
Lo que intenta hacer aquí es lanzar el hilo principal de forma sincrónica desde un hilo de fondo antes de que salga. Este es un error logico
Deberías hacerlo así:
DispatchQueue.global().async(execute: {
print("teste")
DispatchQueue.main.sync{
print("main thread")
}
})
Quiero mostrar un ActivityIndicatorView en mi aplicación, pero cuando llamo al método de sync
desde el hilo principal, la aplicación falla con el error: exc_bad_instruction (code=exc_i386_invop subcode=0x0)
Estoy usando xcode 8.0 y swift 3
¿Puede alguien ayudarme?
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
}