async -> Image { let dataResource = await loadWebResource("dataprofile.txt") // ... } • async: the func can suspend before returning a value (is a coroutine) • await: noop, but shows coder that non-local control flow can happen
: [String] = [] { didSet { mainActor.updateTableView(theList) } } init(mainActor: TheMainActor) { self.mainActor = mainActor } // this checks to see if all the entries in the list are capitalized: // if so, it capitalize the string before returning it to encourage // capitalization consistency in the list. func prettify(_ x : String) -> String { // Details omitted: it inspects theList, adjusting the // string before returning it if necessary. } actor func add(entry: String) { theList.append(prettify(entry)) } }
: [String] = [] { didSet { mainActor.updateTableView(theList) } } func prettify(_ x : String) -> String {} actor func add(entry: String) { theList.append(prettify(entry)) } } // 1. send message to Table actor tableActor.add(entry: “foo") // 2. Table actor modifies its own data // 3. Table actor sends message to Main actor with copied data // 4. Main actor refreshes table view
actor can send messages to any other actor • actor methods are the messages that actors accept • actor methods are implicitly async, so they can freely call async methods and await their results • An actor method cannot return a value, throw an error, or have an inout parameter. • Actor method parameters must produce independent values when copied • Local state and non-actor methods may only be accessed by methods defined lexically on the actor or in an extension to it (whether they are marked actor or otherwise) actor func add(entry: String) mainActor.updateTableView(theList)
and forget actor messages • add error handling in Actor init, not scattered everywhere • handle failures by restarting actor, etc. • Improving system architecture • declarative, clean interprocess communication • `distributed` keyword