2014-11-21 2 views
0

나는 다음과 같은 코드가 있다고 가정 뷰에 추가? 모든 ifelse 지점에모범 사례는

dispatch_async(dispatch_get_main_queue()) { 
    MBProgressHUD.hideHUDForView(self.view, animated: true) 
    return 
} 

코드를 추가하는 것보다이 일을 더 우아한 방법이 있나요?

미리 감사드립니다. 첫째

답변

1

당신의 URL를 초기화하고 바로 작업하기 전에

if let url = NSURL(string: someURL) { 
    MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
    // start the request here 

다음 콜백 블록이

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
       (data, response, error) in 
    dispatch_async(dispatch_get_main_queue()) { 
     MBProgressHUD.hideHUDForView(self.view, animated: true) 
    } 
    // here goes other logic 

당신이 후 return를 호출 할 필요가 없습니다 시작 직후에 그것을 숨길 시작 후 HUD를 표시 허드가 숨겨집니다.