SWIFT: создание второго потока

var workItem: DispatchWorkItem? = nil

...

override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(animated)
        
   let indicator = activityIndicator()
   indicator.startAnimating()
        
   workItem = DispatchWorkItem {
            // долгая команда

   DispatchQueue.main.async {
       indicator.stopAnimating()
       indicator.hidesWhenStopped = true
       }
   }
   DispatchQueue.global(qos: .utility).async(execute: workItem!)
}

...

override func viewDidDisappear(_ animated: Bool) {
    if let workItem = workItem {
        workItem.cancel()
        self.workItem = nil
    }
}
Print Friendly, PDF & Email

Добавить комментарий