.resume在调用其他委托方法后执行

时间:2017-08-02 13:06:44

标签: swift3

我在viewDidLoad()中添加了我的webservice调用,但在调用所有其他函数后它总是恢复执行。以下是我的代码。

override func viewDidLoad() {        
    super.viewDidLoad()
    self.addSlideMenuButton()        
    getData()        
    calendarManager = JTCalendarManager.init()
    calendarManager.delegate = self 
    createMinAndMaxDate()        
    calendarManager.menuView = calendarMenuView
    calendarManager.contentView = calendarContentView
}

func getData(){        
    var request = URLRequest(url: URL(string: "http:xxx")!)
    request.httpMethod = "GET"
    let task = URLSession.shared.dataTask(with: request) { data, 
    response, error in
        guard let data = data, error == nil else {
            print("error=\(String(describing: error))")
            return
        }            
        else {                
            do{                    
                //empty eventDetail dictionary
                self.Detail = [String: String]() as NSDictionary

                let json = try JSONSerialization.jsonObject(with: 
                data, options:.allowFragments) as! [String:AnyObject]
                if let dictionary = json as? [String: Any] {
                    if let nestedDictionary = 
                    dictionary["xxx_webservices"] as? [String: Any] 
                    {
                        // access nested dictionary values by key
                        let dateArray = nestedDictionary["3"] as! 
                        [String:Any]
                        for details in (dateArray["xxx"]  as! 
                        [[String:Any]] ){
                            // access all key / value pairs in 
                         dictionary
                            print(details["xxx"]!)

                                DispatchQueue.main.async (execute: { 
                                () -> Void in
                                    //event details contains details 
                                    of an event
                                    self.eventDetail = 
                                     nestedDictionary["3"] as! 
                                     NSDictionary
                                })
                            }
                        }
                    }
                }                    
            }
            catch {
                print("Error with Json: \(error)")
            }                
        }            
    }
    task.resume()

task.resume()总是在调用calendar方法的委托后执行。如何在调用其他代表之前恢复任务,以便我可以在日历视图中显示数据? 谢谢

1 个答案:

答案 0 :(得分:0)

在Web服务中添加此代码就可以了。

DispatchQueue.main.async {
    self.calendarManager.reload()
} 

这将使用Web服务数据调用日历代理。