I’m new to Swift structured concurrency and Actors.
I think I’ve correctly used Task in an Actor, but it just feels… too simple? Which is an amazing review of Swift Concurrency if I in fact did get it right?!
extension POWActor {
func work() async throws -> POW {
try await Task<POW, Swift.Error>(priority: .userInitiated) {
while true {
guard
Date() < config.deadline
else {
throw SHA256TwicePOWWorker.Error.timeout
}
updateOutputByHashingNonce()
if state.output.leadingZeroBitCount() >= config.difficulty {
return POW(
state: state,
config: config,
input: input
)
} else {
state.nonce += 1
}
}
}.value
}
}
This is a simple Proof of Work implementation, you find the Swift Package Arbeta here.
So my questions:
Will this in fact run in the background (the Actor is not marked as @MainActor
?
Is the priority userInitiated
correct? My idea is that somewhere in some app a user initiated an action, which required PoW?