You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: stdlib/public/Concurrency/Task.swift
+27-6Lines changed: 27 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ import Swift
78
78
/// and additional reasons can accrue during the cancellation process.
79
79
///
80
80
/// ### Task closure lifetime
81
-
/// A task is initialized with a closure. containing the code that the task executes.
81
+
/// Tasks are initialized by passing a closure containing the code that will be executed by a given task.
82
82
///
83
83
/// After this code has run to completion, the task has completed, resulting in either
84
84
/// a failure or result value, this closure is eagerly released.
@@ -88,7 +88,7 @@ import Swift
88
88
/// after the task completes.
89
89
/// Consequently, tasks rarely need to capture weak references to values.
90
90
///
91
-
/// For example, in this example, it is not necessary to capture the actor as weak,
91
+
/// For example, in the following snippet of code it is not necessary to capture the actor as `weak`,
92
92
/// because as the task completes it'll let go of the actor reference, breaking the
93
93
/// reference cycle between the Task and the actor holding it.
94
94
///
@@ -100,21 +100,42 @@ import Swift
100
100
/// var result: Work?
101
101
///
102
102
/// deinit {
103
-
/// print("deinit")
103
+
/// assert(work != nil)
104
+
/// // even though the task is still retained,
105
+
/// // once it completes it no longer causes a reference cycle with the actor
106
+
///
107
+
/// print("deinit actor")
104
108
/// }
105
109
///
106
110
/// func start() {
107
111
/// work = Task {
112
+
/// print("start task work")
108
113
/// try? await Task.sleep(for: .seconds(3))
109
-
/// self.work = Work() // we captured self
114
+
/// self.result = Work() // we captured self
115
+
/// print("completed task work")
110
116
/// // but as the task completes, this reference is released
111
117
/// }
118
+
/// // we keep a strong reference to the task
112
119
/// }
113
120
/// }
121
+
/// ```
122
+
///
123
+
/// And using it like this:
114
124
///
125
+
/// ```
115
126
/// await Actor().start()
116
-
/// // no references to Actor other than inside the Task
117
-
/// // as the Task completes; the closure is destroyed releasing the actor.
127
+
/// ```
128
+
///
129
+
/// Note that there is nothing, other than the Task's use of `self` retaining the actor,
130
+
/// And that the start method immediately returns, without waiting for the unstructured `Task` to finish.
131
+
/// So once the task completes and its the closure is destroyed, the strong reference to the "self" of the actor is also released allowing the actor to deinitialize as expected.
132
+
///
133
+
/// Therefore, the above call will consistently result in the following output:
0 commit comments