@@ -16,9 +16,8 @@ for example, a computer with a four-core processor
16
16
can run four pieces of code at the same time,
17
17
with each core carrying out one of the tasks.
18
18
A program that uses parallel and asynchronous code
19
- carries out multiple operations at a time;
20
- it suspends operations that are waiting for an external system,
21
- and makes it easier to write this code in a memory-safe way.
19
+ carries out multiple operations at a time,
20
+ and it suspends operations that are waiting for an external system.
22
21
23
22
The additional scheduling flexibility from parallel or asynchronous code
24
23
also comes with a cost of increased complexity.
@@ -345,6 +344,14 @@ so you can call it from synchronous code and wait for the result.
345
344
The Swift standard library intentionally omits this unsafe functionality,
346
345
and trying to implement it yourself can lead to
347
346
problems like subtle races, threading issues, and deadlocks.
347
+ When adding concurrent code to an existing project,
348
+ work from the top down.
349
+ Specifically,
350
+ start by converting the topmost layer of code to use concurrency,
351
+ and then start converting the functions and methods that it calls,
352
+ working through the project's architecture one layer at a time.
353
+ There's no way to take a bottom-up approach,
354
+ because synchronous code can't ever call asynchronous code.
348
355
349
356
<!--
350
357
OUTLINE
@@ -655,7 +662,7 @@ Each task checks whether it has been canceled
655
662
at the appropriate points in its execution,
656
663
and responds to cancellation an appropriate way.
657
664
Depending on the work you're doing,
658
- that usually means one of the following:
665
+ responding to cancellation usually means one of the following:
659
666
660
667
- Throwing an error like ` CancellationError `
661
668
- Returning ` nil ` or an empty collection
@@ -1044,7 +1051,7 @@ only code running on an actor can access that actor's local state.
1044
1051
This guarantee is known as * actor isolation* .
1045
1052
1046
1053
The following aspects of the Swift concurrency model
1047
- combine to make it easier to reason about shared mutable state:
1054
+ work together to make it easier to reason about shared mutable state:
1048
1055
1049
1056
- Code in between possible suspension points runs sequentially,
1050
1057
without the possibility of interruption from other concurrent code.
@@ -1071,7 +1078,7 @@ extension TemperatureLogger {
1071
1078
}
1072
1079
```
1073
1080
1074
- The code above converts one measurement at a time.
1081
+ The code above converts the array of measurements, one at a time.
1075
1082
While the map operation is in progress,
1076
1083
some temperatures are in Fahrenheit and others are in Celsius.
1077
1084
However, because none of the code includes ` await ` ,
@@ -1083,8 +1090,10 @@ This means there's no way for other code
1083
1090
to read a list of partially converted temperatures
1084
1091
while unit conversion is in progress.
1085
1092
1086
- The ` convertFarenheitToCelsius() ` method makes an even stronger guarantee:
1087
- It's a synchronous method,
1093
+ In addition to writing code in an actor
1094
+ that protects temporary invalid state by omitting potential suspension points,
1095
+ you can move that code into a synchronous method.
1096
+ The ` convertFarenheitToCelsius() ` method above is method,
1088
1097
so it's guaranteed to * never* contain potential suspension points.
1089
1098
This function encapsulates the code
1090
1099
that temporarily makes the data model inconsistent,
0 commit comments