2017-01-03 2 views
0

이전에 액세스가 거부 된 경우 장치 기능을 사용하려면 설정에 액세스하는 것이 가장 바람직하지 않습니다. 하나는 스레딩을 사용하는 두 가지 방법을 보았습니다. 그러나 나는 왜 명확한 설명을 발견하지 못했습니까? 이것에 대한 통찰력을 고맙게 생각합니다. 감사.설정에 액세스하여 장치 기능에 액세스 할 수있는 이유는 무엇입니까?

두 작품, 어느 쪽 경고 컨트롤러의 작업으로 아래의 조각 :

alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
      //open settings to allow access to geo 
      guard let stngUrl = URL(string: UIApplicationOpenSettingsURLString) else { 
       return 
      } 
      if UIApplication.shared.canOpenURL(stngUrl) { 
       if #available(iOS 10.0, *) { 
        UIApplication.shared.open(stngUrl, completionHandler: { (success) in 
        }) 
       } else { 
        if UIApplication.shared.canOpenURL(stngUrl) { 
         UIApplication.shared.openURL(stngUrl) 
        } 
       } 
      }     
     })) 
     present(alert, animated: true, completion: nil) 

이하 조각처럼 DispatchQueue를 사용하고 계십니까?

alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: {(_ action: UIAlertAction) -> Void in    
     DispatchQueue.main.async(execute: {() -> Void in 
      UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!) 
     }) 
    })) 
    present(alert, animated: true, completion: nil) 

답변

0

는 핸들러가 항상 주 스레드에서 호출 될 것이기 때문에 UIAlertAction의 핸들러에 DispatchQueue.main를 사용할 필요가 없습니다. 따라서 DispatchQueue.main을 사용하는 것은 불필요합니다.

+0

안녕하십니까, 답장을 보내 주셔서 감사합니다. 귀하가하는 말은 흥미 롭습니다. 다음과 같이 불필요한 것을 말하고 있습니까? http://stackoverflow.com/a/37420335/5601401 – TheeBen

+0

거기에 있으면 안됩니다. 아니요. – rmaddy

+0

또한, 여기에 내가 뭔가 혼란 스럽다. UIAlertAction의 처리기가 주 스레드에 있다면 왜 이런 식으로해야 할까? http://stackoverflow.com/a/31086823/5601401 – TheeBen

관련 문제