I use the function of Parsing data from the site

func pars(arg: Bool, completion: (Bool) -> ()) { do { myURL = URL(string: "https:_______") let html = try! String(contentsOf: myURL!, encoding: .utf8) let doc: Document = try SwiftSoup.parse(html) let body = doc.body() name = (try body?.select(".name"))! completion(arg) } catch Exception.Error(let type, let message) { print(message) } catch { print("error") } } 

But when you minimize the application often crashes, since the data can not be loaded. so, is it possible to stop the function or continue its execution but already in the background?

  • If the data has already arrived, maybe you should just re-launch this method, and if you go to the background just cancel it, or do you want to wait for a response from the server in the background? - Victor Mishustin
  • I did all the same re-load the function on error, when I exit the background it gives an error, when I enter the active state, the function is loaded again. And how to make a cancellation when going to the background? - Vladislav Bublik

0