{"id":327244,"date":"2022-01-10T08:49:03","date_gmt":"2022-01-10T08:49:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=327244"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=327244","title":{"rendered":"<span>Async\/await \u0434\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 iOS-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/ws\/mu\/4u\/wsmu4uuzmzjceeadkcqtavkqiga.png\"\/><\/p>\n<p>  \u0420\u0430\u043d\u0435\u0435 \u044f \u043f\u0438\u0441\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u044e <a href=\"https:\/\/www.artemnovichkov.com\/blog\/async-await-offline#:~:text=working%20with%20web%20view%20content%20offline\">\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u043e\u0444\u0444\u043b\u0430\u0439\u043d \u0441 \u0432\u0435\u0431-\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u043e\u043c<\/a>. \u0421 \u0442\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 Apple \u0432\u044b\u043f\u0443\u0441\u0442\u0438\u043b\u0430 Xcode 13.2 \u0438 Swift 5.5. \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u043a\u043d\u0438\u0433\u0443 <a href=\"https:\/\/www.artemnovichkov.com\/blog\/async-await-offline#:~:text=modern%20concurrency%20model%20in%20Swift\">\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 Swift<\/a>, \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u044d\u0442\u043e \u043b\u0443\u0447\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0441 async\/await!<br \/>  \u041f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u0447\u0435\u043d\u044c \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/Concurrency.html\">\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043e \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 Swift Language Guide.<\/a><br \/>  <a name=\"habracut\"><\/a><br \/>  <i>\u0417\u0430\u043c\u0435\u0442\u043a\u0430: \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0430 Swift 5.5 \u0438 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043d\u0430 iOS 15.0 \u0441 Xcode 13.2.<\/i><\/p>\n<h3>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h3>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0431\u0435\u0436\u0438\u043c\u0441\u044f \u043f\u043e \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <code>WebDataManager<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0432\u0435\u0431 \u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u043f\u043e URL:<\/p>\n<pre><code class=\"swift\">import WebKit  final class WebDataManager: NSObject {          enum DataError: Error {         case noImageData     }          \/\/ 1     enum DataType: String, CaseIterable {         case snapshot = \"Snapshot\"         case pdf = \"PDF\"         case webArchive = \"Web Archive\"     }          \/\/ 2     private var type: DataType = .webArchive          \/\/ 3     private lazy var webView: WKWebView = {         let webView = WKWebView()         webView.navigationDelegate = self         return webView     }()          private var completionHandler: ((Result&lt;Data, Error>) -> Void)?          \/\/ 4     func createData(url: URL, type: DataType, completionHandler: @escaping (Result&lt;Data, Error>) -> Void) {         self.type = type         self.completionHandler = completionHandler         webView.load(.init(url: url))     } } <\/code><\/pre>\n<p>  \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c:  <\/p>\n<ul>\n<li>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0442\u0438\u043f <code>DataType<\/code> \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445;<\/li>\n<li>\u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code> type<\/code> \u0441 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f;<\/li>\n<li>\u0421\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>webView<\/code> \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445;<\/li>\n<li>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>createData<\/code> \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 dataType, completionHandler \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u0435\u0431-\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e URL.<\/li>\n<\/ul>\n<p>  \u0427\u0435\u0433\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442? \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <code>WKNavigationDelegate<\/code>:<\/p>\n<pre><code class=\"swift\">extension WebDataManager: WKNavigationDelegate {          func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {         switch type {         case .snapshot:             let config = WKSnapshotConfiguration()             config.rect = .init(origin: .zero, size: webView.scrollView.contentSize)             webView.takeSnapshot(with: config) { [weak self] image, error in                 if let error = error {                     self?.completionHandler?(.failure(error))                     return                 }                 guard let pngData = image?.pngData() else {                     self?.completionHandler?(.failure(DataError.noImageData))                     return                 }                 self?.completionHandler?(.success(pngData))             }         case .pdf:             let config = WKPDFConfiguration()             config.rect = .init(origin: .zero, size: webView.scrollView.contentSize)             webView.createPDF(configuration: config) { [weak self] result in                 self?.completionHandler?(result)             }         case .webArchive:             webView.createWebArchiveData { [weak self] result in                 self?.completionHandler?(result)             }         }     }          func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {         completionHandler?(.failure(error))     } }<\/code><\/pre>\n<p>  \u0418\u0442\u043e\u0433\u043e: \u0443 \u043d\u0430\u0441 6 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <code>completionHandler<\/code> \u0438 \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 <code>self<\/code> \u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u043e\u0432 \u0443\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f. \u041c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u0443\u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 c async\/await? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c!<\/p>\n<h3>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430<\/h3>\n<p>  \u041c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>createData<\/code> \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435:<\/p>\n<pre><code class=\"swift\">func createData(url: URL, type: DataType) async throws -> Data<\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0432\u0435\u0431-\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u043e\u043c, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u0432 webview \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0435\u0435 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>webView(_:didFinish:)<\/code> \u0443 <code>WKNavigationDelegate<\/code>. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>withCheckedThrowingContinuation<\/code>, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u0443 \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0439 \u0441 async\\await.<br \/>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u0435\u0431-\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 URL:<\/p>\n<pre><code class=\"swift\">private var continuation: CheckedContinuation&lt;Void, Error>?  private func load(_ url: URL) async throws {     return try await withCheckedThrowingContinuation { continuation in         self.continuation = continuation         self.webView.load(.init(url: url))     } }<\/code><\/pre>\n<p>  \u041c\u044b \u0445\u0440\u0430\u043d\u0438\u043c <code>continuation<\/code>, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430. \u041c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>continuation<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"swift\">extension WebDataManager: WKNavigationDelegate {          func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {         continuation?.resume(returning: ())     }          func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {         continuation?.resume(throwing: error)     } } <\/code><\/pre>\n<p>  \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434, \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043e\u0448\u0438\u0431\u043a\u0443:<br \/>  <i>Call to main actor-isolated instance method &#8216;load&#8217; in a synchronous nonisolated context<\/i><\/p>\n<p>  \u041c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>MainActor<\/code>, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0447\u0438\u043d\u0438\u0442\u044c \u044d\u0442\u043e:<\/p>\n<pre><code class=\"swift\">@MainActor private func load(_ url: URL) async throws {   \/\/ implementation }<\/code><\/pre>\n<p>  MainActor \u2013 \u044d\u0442\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u043a\u0442\u043e\u0440, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043a\u043e\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0412\u0441\u0435 <code>UIView<\/code> (\u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0438 <code>WKWebView<\/code>) \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>load<\/code>:<\/p>\n<pre><code class=\"swift\">@MainActor func createData(url: URL, type: DataType) async throws -> Data {     try await load(url)     \/\/ To be implemented     return Data() }<\/code><\/pre>\n<p>  \u041c\u044b \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>createData<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 <code>MainActor<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>load<\/code> \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0432 \u043a\u043b\u0430\u0441\u0441 <code>WebDataManager<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u0441\u0435\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439:<\/p>\n<pre><code class=\"swift\">@MainActor  final class WebDataManager: NSObject {     \/\/ implementation }<\/code><\/pre>\n<h3>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u043c\u0438 API \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e async\/await<\/h3>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0433\u043e\u0442\u043e\u0432\u044b \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0435\u0431-\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u0430. \u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 PDF:<\/p>\n<pre><code class=\"swift\">let config = WKPDFConfiguration() config.rect = .init(origin: .zero, size: webView.scrollView.contentSize) webView.createPDF(configuration: config) { [weak self] result in     self?.completionHandler?(result) }<\/code><\/pre>\n<p>  \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u043a\u043e\u043c\u0430\u043d\u0434\u0430 Apple \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0430 async\/await \u0430\u043d\u0430\u043b\u043e\u0433\u0438 \u0434\u043b\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 \u043a\u043e\u043b\u043b\u0431\u0435\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"swift\">let config = WKPDFConfiguration() config.rect = .init(origin: .zero, size: webView.scrollView.contentSize) return try await webView.pdf(configuration: config)<\/code><\/pre>\n<p>  \u041e\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438, \u043e\u0434\u043d\u0430\u043a\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 web-\u0430\u0440\u0445\u0438\u0432\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043a\u043e\u043b\u043b\u0431\u0435\u043a\u043e\u043c. \u0417\u0434\u0435\u0441\u044c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>withCheckedThrowingContinuation<\/code>:<\/p>\n<pre><code class=\"swift\">import WebKit  extension WKWebView {      func webArchiveData() async throws -> Data {         try await withCheckedThrowingContinuation { continuation in             createWebArchiveData { result in                 continuation.resume(with: result)             }         }     } }<\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e <code>continuation<\/code> \u043c\u043e\u0436\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>Result<\/code> \u0438 \u0435\u0433\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<br \/>  \u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>createData<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043b\u0443\u0447\u0448\u0435:<\/p>\n<pre><code class=\"swift\">func createData(url: URL, type: DataType) async throws -> Data {     try await load(url)     switch type {     case .snapshot:         let config = WKSnapshotConfiguration()         config.rect = .init(origin: .zero, size: webView.scrollView.contentSize)         let image = try await webView.takeSnapshot(configuration: config)         guard let pngData = image.pngData() else {             throw DataError.noImageData         }         return pngData     case .pdf:         let config = WKPDFConfiguration()         config.rect = .init(origin: .zero, size: webView.scrollView.contentSize)         return try await webView.pdf(configuration: config)     case .webArchive:         return try await webView.webArchiveData()     } }<\/code><\/pre>\n<p>  \u041c\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u043c\u0435\u0441\u0442\u0430 \u0437\u0430\u0445\u0432\u0430\u0442\u0430 <code>self<\/code> \u0432 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u0445.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u044b\u0445 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439<\/h3>\n<p>  \u0423\u0440\u0430, \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u044d\u0442\u043e! \u041f\u043e\u0433\u043e\u0434\u0438\u0442\u0435, \u043d\u043e \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430? \u0421 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Task<\/code> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<pre><code class=\"swift\">Task {     do {         let url = URL(string: \"https:\/\/www.artemnovichkov.com\")!         let data = try await webDataManager.createData(url: url, type: .pdf)         print(data)     }     catch {         print(error)     } }<\/code><\/pre>\n<p>  \u0424\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 <a href=\"https:\/\/github.com\/artemnovichkov\/OfflineDataAsyncExample\">OfflineDataAsyncExample <\/a> \u043d\u0430 Github.<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043d\u043e\u0432\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0430\u0445\u0430\u0440. \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0431\u043e\u043b\u0435\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u043c\u0443 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u043a\u043e\u0434\u0443. \u041c\u044b \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u0435\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0437\u0430\u0445\u0432\u0430\u0442\u0430 <code>self<\/code> \u0432 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u0445 \u0438 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043e\u043a. \u042f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u00ab\u0438\u0433\u0440\u0430\u0442\u044c\u00bb \u0441 async\/await \u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 <a href=\"https:\/\/github.com\/artemnovichkov\/awesome-swift-async-await\">awesome-swift-async-await<\/a>. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434, \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u043b\u044e\u0431\u0438\u043c\u044b\u043c\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430\u043c\u0438 \u043f\u043e \u044d\u0442\u043e\u0439 \u0442\u0435\u043c\u0435!<\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/skyeng\/blog\/598295\/\"> https:\/\/habr.com\/ru\/company\/skyeng\/blog\/598295\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\" class=\"article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" data-src=\"https:\/\/habrastorage.org\/webt\/ws\/mu\/4u\/wsmu4uuzmzjceeadkcqtavkqiga.png\"\/><\/p>\n<p>  \u0420\u0430\u043d\u0435\u0435 \u044f \u043f\u0438\u0441\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u044e <a href=\"https:\/\/www.artemnovichkov.com\/blog\/async-await-offline#:~:text=working%20with%20web%20view%20content%20offline\">\u043e \u0440\u0430\u0431\u043e\u0442\u0435 \u043e\u0444\u0444\u043b\u0430\u0439\u043d \u0441 \u0432\u0435\u0431-\u043a\u043e\u043d\u0442\u0435\u043d\u0442\u043e\u043c<\/a>. \u0421 \u0442\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 Apple \u0432\u044b\u043f\u0443\u0441\u0442\u0438\u043b\u0430 Xcode 13.2 \u0438 Swift 5.5. \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u043a\u043d\u0438\u0433\u0443 <a href=\"https:\/\/www.artemnovichkov.com\/blog\/async-await-offline#:~:text=modern%20concurrency%20model%20in%20Swift\">\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 Swift<\/a>, \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u044d\u0442\u043e \u043b\u0443\u0447\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0441 async\/await!<br \/>  \u041f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043e\u0447\u0435\u043d\u044c \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/Concurrency.html\">\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043e \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 Swift Language Guide.<\/a>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-327244","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=327244"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327244\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=327244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=327244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=327244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}