A distributed actor is a special kind of actor designed to work across different processes or machines, allowing safe and structured communication between them.
Click to reveal answer
intermediate
How do distributed actors differ from regular actors in Swift?
Distributed actors can communicate across network boundaries and handle remote calls, while regular actors work only within the same process.
Click to reveal answer
beginner
What keyword is used to declare a distributed actor in Swift?
The keyword distributed actor is used to declare a distributed actor.
Click to reveal answer
intermediate
What is the role of the id property in distributed actors?
The id property uniquely identifies a distributed actor instance across the network, enabling remote references.
Click to reveal answer
beginner
Why must distributed actor methods be asynchronous?
Because calls to distributed actors may involve network communication, their methods are asynchronous to handle delays and possible failures safely.
Click to reveal answer
Which keyword declares a distributed actor in Swift?
Aremote actor
Bdistributed actor
Cnetwork actor
Dasync actor
✗ Incorrect
The correct keyword is distributed actor to declare a distributed actor.
What type of methods do distributed actors require for their remote calls?
ASynchronous methods
BStatic methods
CAsynchronous methods
DPrivate methods
✗ Incorrect
Distributed actor methods must be asynchronous because remote calls can take time and may fail.
What does the id property in a distributed actor represent?
AThe actor's local memory address
BThe actor's type name
CThe actor's creation timestamp
DA unique identifier for the actor instance across the network
✗ Incorrect
The id uniquely identifies the distributed actor instance across different machines.
Which of these is NOT a feature of distributed actors?
ADirect memory sharing between actors
BRemote method calls across machines
CAutomatic network failure handling
DSafe concurrency within a single process
✗ Incorrect
Distributed actors do not share memory directly; they communicate via messages over the network.
Why are distributed actors useful?
AThey allow safe, structured communication between processes or machines
BThey make synchronous calls faster
CThey replace all network protocols
DThey simplify writing concurrent code only on one device
✗ Incorrect
Distributed actors help write code that communicates safely and clearly across different devices or processes.
Explain what distributed actors are and why they are important in Swift concurrency.
Think about how actors work locally versus across machines.
You got /4 concepts.
Describe the role of the id property in distributed actors and how it helps remote communication.
Consider how you find a friend in a big city using their unique address.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a distributed actor in Swift?
easy
A. To define a class that cannot be used with concurrency
B. To create a local-only actor with no network communication
C. To allow an actor to work across multiple computers easily
D. To replace all functions with synchronous calls
Solution
Step 1: Understand the role of distributed actors
Distributed actors are designed to work across different machines, enabling remote communication.
Step 2: Compare options with the concept
Only To allow an actor to work across multiple computers easily correctly describes this purpose; others describe unrelated or incorrect features.
Final Answer:
To allow an actor to work across multiple computers easily -> Option C
Quick Check:
Distributed actor purpose = cross-computer use [OK]
Hint: Distributed actors enable remote work across computers [OK]
Common Mistakes:
Thinking distributed actors are local-only
Confusing distributed actors with regular classes
Assuming distributed actors disable concurrency
2. Which of the following is the correct way to declare a distributed function inside a distributed actor?
easy
A. func fetchData() async throws -> String
B. func distributed fetchData() async throws -> String
C. distributed func fetchData() -> String
D. distributed func fetchData() async throws -> String
Solution
Step 1: Recall distributed function syntax
Distributed functions inside distributed actors use the keyword distributed func and are usually async and throwable.
distributed func fetchData() async throws -> String -> Option D
Quick Check:
Distributed function syntax = distributed func [OK]
Hint: Use 'distributed func' keyword for remote calls [OK]
Common Mistakes:
Omitting 'distributed' keyword
Placing 'distributed' after 'func'
Forgetting async or throws in distributed funcs
3. Given the code below, what will be printed when calling await greeter.sayHello()?
distributed actor Greeter {
distributed func sayHello() async -> String {
return "Hello from distributed actor!"
}
}
let greeter = Greeter()
Task {
let message = await greeter.sayHello()
print(message)
}
medium
A. Error: distributed actor cannot be instantiated directly
B. Hello from distributed actor!
C. Hello from local actor!
D. Runtime error due to missing network connection
Solution
Step 1: Understand distributed actor instantiation
Distributed actors can be instantiated locally for testing or local use, so Greeter() is valid here.
Step 2: Analyze the distributed function call
The sayHello() function returns the string "Hello from distributed actor!" asynchronously, so the print will output this string.
Final Answer:
Hello from distributed actor! -> Option B
Quick Check:
Distributed func returns greeting string [OK]
Hint: Distributed actors can be called locally for testing [OK]
Common Mistakes:
Assuming distributed actors cannot be instantiated locally
Expecting network errors without remote setup
Confusing distributed actor with local-only actor
4. Identify the error in the following distributed actor code:
distributed actor Counter {
var count = 0
distributed func increment() {
count += 1
}
}
medium
A. Distributed functions must be async and throwable
B. Distributed actors cannot have mutable state
C. The distributed func must return a value
D. The count variable must be declared as distributed
Solution
Step 1: Check distributed function requirements
Distributed functions must be declared as async and can throw errors because they involve remote calls.
Step 2: Analyze the given function
The increment() function is missing async and throws keywords, which is a syntax error for distributed functions.
Final Answer:
Distributed functions must be async and throwable -> Option A
Quick Check:
Distributed func requires async and throws [OK]
Hint: Always add async and throws to distributed funcs [OK]
Common Mistakes:
Forgetting async keyword in distributed funcs
Assuming distributed funcs can be synchronous
Thinking distributed actors disallow mutable vars
5. You want to build a distributed chat app using Swift's distributed actors. Which approach correctly uses distributed actors and functions to send a message to a remote user?
hard
A. Define a distributed actor ChatUser with a distributed func sendMessage(_ message: String) that can be called remotely.
B. Use a regular class with async functions and manually handle network communication.
C. Define a distributed actor but use non-distributed functions for sending messages.
D. Use global functions with no actors to send messages asynchronously.
Solution
Step 1: Understand distributed actors for remote communication
Distributed actors are designed to simplify remote calls, so defining a ChatUser as a distributed actor with distributed functions fits the use case.
Step 2: Evaluate options for remote message sending
Define a distributed actor ChatUser with a distributed func sendMessage(_ message: String) that can be called remotely. correctly uses distributed actors and distributed functions for remote calls. Options B, C, and D either ignore distributed actors or misuse function types, making remote communication harder or incorrect.
Final Answer:
Define a distributed actor ChatUser with a distributed func sendMessage(_ message: String) that can be called remotely. -> Option A