<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>.task - 标签 - 宋明的博客</title><link>https://apolla.cc/tags/.task/</link><description>宋明的博客</description><generator>Hugo 0.170.0 &amp; FixIt v1.0.0-rc.5-mwimukpl</generator><language>zh-cn</language><lastBuildDate>Fri, 19 Apr 2024 15:20:05 +0800</lastBuildDate><atom:link href="https://apolla.cc/tags/.task/index.xml" rel="self" type="application/rss+xml"/><item><title>1.3 SwiftUI 中常见的异步 API</title><link>https://apolla.cc/posts/1.3-swiftui-%E4%B8%AD%E5%B8%B8%E8%A7%81%E7%9A%84%E5%BC%82%E6%AD%A5-api/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/1.3-swiftui-%E4%B8%AD%E5%B8%B8%E8%A7%81%E7%9A%84%E5%BC%82%E6%AD%A5-api/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="task"&gt;&lt;span&gt;.task&lt;/span&gt;&#10; &lt;a href="#task" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;SwiftUI 刚发布时，View 只有两个最基础的生命周期方法：&lt;code&gt;.onAppear&lt;/code&gt; 和 &lt;code&gt;.onDisAppear&lt;/code&gt;，iOS 14 又新增了 &lt;code&gt;.onChange&lt;/code&gt; 这样的修饰器，可以监听值的变化。不过上面的几个 API 都是在主线程调用的，iOS 15 新增的 &lt;code&gt;.task&lt;/code&gt; 修饰器赋予了 View 快速开启异步任务的能力。&lt;/p&gt;</description></item><item><title>2.1 任务</title><link>https://apolla.cc/posts/2.1-%E4%BB%BB%E5%8A%A1/</link><pubDate>Fri, 19 Apr 2024 15:20:05 +0800</pubDate><guid>https://apolla.cc/posts/2.1-%E4%BB%BB%E5%8A%A1/</guid><category domain="https://apolla.cc/categories/swiftui/">SwiftUI</category><description>&lt;h1 class="heading-element" id="任务的创建"&gt;&lt;span&gt;任务的创建&lt;/span&gt;&#10; &lt;a href="#%e4%bb%bb%e5%8a%a1%e7%9a%84%e5%88%9b%e5%bb%ba" class="heading-mark"&gt;&lt;svg class="octicon octicon-link" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"&gt;&lt;path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/a&gt;&#10;&lt;/h1&gt;&lt;p&gt;在前面的章节中，我们已经使用过 &lt;code&gt;Task&lt;/code&gt; 了，比如在同步环境 &lt;code&gt;viewDidLoad&lt;/code&gt; 方法中开启异步环境，或者使用 View 的 &lt;code&gt;.task&lt;/code&gt; 修饰器开启异步任务。还有 &lt;code&gt;async let&lt;/code&gt; 这样的语法糖，也会隐式地创建 task，方便我们写出简洁、安全的并发代码。&lt;/p&gt;</description></item></channel></rss>