2017-03-01 2 views
-1

비동기 요청을 보내고 성공하면보기 컨트롤러를 표시합니다. 그것은 작동합니다.백그라운드에서 비동기로 현재보기 컨트롤러

하지만 성공할 때 내 앱이 배경에 있고 전경에서 전달할 때 문제가 발생합니다. 보기 컨트롤러가 항상 표시되는 것은 아닙니다.

주 스레드에 관한 것 같지만 잘 모르겠습니다.

어떻게 해결할 수 있습니까?

편집 : 당신이 배경에서 UI 기능을 호출하는 경우

func showPopup(on viewController: UIViewController) { 
    let viewControllerToPresent = MyPopupViewController(nibName: "Popup", bundle: nil) 

    let popup = PopupDialog(viewController: viewControllerToPresent, buttonAlignment: .horizontal, transitionStyle: .zoomIn, gestureDismissal: false) 

    let button = DefaultButton(title: "Ok") { 
     popup.dismiss(animated: true, completion: nil) 
    } 

    popup.addButtons([button]) 
    viewController.present(popup, animated: true, completion: nil) 
} 
+0

코드는 어디에 있습니까? 너 무슨 짓을 한거야? – Mannopson

+0

위의 코드는 백그라운드에서 호출되는 것을 표시하지 않습니다. _relevant_ 코드를 모두 표시하십시오. 감사합니다 –

+0

다른 관련 코드는 없습니다. – Grifas

답변

2

는, 즉, 애플이 당신을 허용하지 않습니다 일시 중단 사용자 인터페이스에 실질적인 변경을가합니다. 이 경우 가장 좋은 방법은 아마도 반환 할 작업을하고 App Delegates applicationDidBecomeActive 메서드를 체크하고 싶을 것입니다.

+0

예, 응용 프로그램이 백그라운드에 있으면 willEnterForeground에 옵서버를 추가하고 물건을 저장하면 저장한다고 생각합니다. 답변 주셔서 감사합니다 :) – Grifas

0

이 결과는 예측할 수 : 여기

내가 성공 후 호출하는 기능입니다. 주 스레드에 명시 적으로 표시됩니다. 다음은 간단한 예입니다 :

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.doBackgroundStuff() 
    } 

    func getThread() -> String { 
     return Thread.isMainThread ? "UI" : "BG" 
    } 

    func doBackgroundStuff() { 
     // force to background 
     DispatchQueue.global().async { 
      print("doBackgroundStuff: this is the \(self.getThread()) thread") 
      self.showPopup(on: self) 
     } 
    } 

    func showPopup(on viewController: UIViewController) { 
     print("showPopup OUTER: this is the \(self.getThread()) thread") 
     // force to foreground 
     DispatchQueue.main.sync { 
      print("showPopup INNER: this is the \(self.getThread()) thread") 
      let popup = PresentingViewController(nibName: "PresentingViewController", bundle: nil) 
      viewController.present(popup, animated: true, completion: nil) 
     } 
    } 
} 
UI는 표시되고 콘솔의 출력은

: 응용 프로그램이 백그라운드에있을 때

doBackgroundStuff: this is the BG thread 
showPopup OUTER: this is the BG thread 
showPopup INNER: this is the UI thread 
+0

내 문제는 비동기 요청 중에 앱이 백그라운드에있는 경우입니다 – Grifas

+0

앱이있을 때 뷰 컨트롤러를 제공 할 수는 없습니다. 백그라운드에서. 이것은 애플의 지침에 위배된다. –

관련 문제