Challenge - 5 Problems
Distributed Actor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple distributed actor method call
What is the output of this Swift code using a distributed actor?
Swift
distributed actor Counter {
var value = 0
distributed func increment() async {
value += 1
}
func current() -> Int {
return value
}
}
func test() async {
let counter = Counter()
await counter.increment()
print(counter.current())
}
task {
await test()
}Attempts:
2 left
💡 Hint
Remember that distributed functions are asynchronous and may not update local state immediately.
✗ Incorrect
The distributed func increment() is asynchronous and runs remotely, so the local value is not updated immediately. The current() method returns the local value, which remains 0.
🧠 Conceptual
intermediate1:30remaining
Understanding the role of distributed actor identity
What is the main purpose of the identity property in a distributed actor?
Attempts:
2 left
💡 Hint
Think about how distributed actors communicate remotely.
✗ Incorrect
The identity property uniquely identifies a distributed actor instance across the network, allowing remote calls to be routed correctly.
🔧 Debug
advanced2:00remaining
Identify the error in distributed actor initialization
What error will this code produce and why?
Swift
distributed actor Logger {
let id: UUID
init() {
id = UUID()
}
}
func test() {
let logger = Logger()
}Attempts:
2 left
💡 Hint
Distributed actors need a system to manage their identity and communication.
✗ Incorrect
Distributed actors require an actor system parameter in their initializer to manage identity and remote communication. The code lacks this parameter, causing a compilation error.
📝 Syntax
advanced1:30remaining
Correct syntax for distributed function declaration
Which option correctly declares a distributed function in a distributed actor?
Swift
distributed actor Service {
???
}Attempts:
2 left
💡 Hint
The 'distributed' keyword comes before 'func'.
✗ Incorrect
The correct syntax places 'distributed' before 'func' and supports async and throws modifiers.
🚀 Application
expert2:30remaining
Predict the number of items in a distributed actor's dictionary after remote calls
Given this distributed actor code, how many key-value pairs will the dictionary contain after the calls?
Swift
distributed actor Store {
var items: [String: Int] = [:]
distributed func addItem(key: String, value: Int) async {
items[key] = value
}
func countItems() -> Int {
return items.count
}
}
func test() async {
let store = Store()
await store.addItem(key: "a", value: 1)
await store.addItem(key: "b", value: 2)
print(store.countItems())
}
task {
await test()
}Attempts:
2 left
💡 Hint
Distributed functions run remotely and do not update local state immediately.
✗ Incorrect
The distributed func addItem runs remotely and updates the remote state, but the local 'items' dictionary remains empty. The countItems() method returns the local count, which is 0.