I feel like I'm missing something obvious here, but I’m not sure how to best implement what I’m trying to do. Maybe an Actor is not what’s needed here.
I'm trying to coordinate logging messages among a bunch of different Loggers (this is being shoehorned into swift-log
, but that implementation detail shouldn't be relevant). I have an arbitrary number of LogHandler
s that share a singleton actor LogCoordinator
. I need to be able to call a nonisolated log
method on the LogCoordinator
from any context, and the calls can be executed asynchronously, but should be serialized. To that end, I’m trying to do something like this:
actor LogCoordinator
{
nonisolated func log(message: String)
{
self.workQ.async
{
await self.logInternal(message: message)
}
}
func logInternal(message: String)
{
// do stuff with internal state
}
}
But I can't actually await
within a DispatchQueue.async
call. I have to wrap it in a Task
, but that strikes me as expensive. Is it expensive?
Am I going about this entirely the wrong way? If this were just a simple class
, I could manage the queues myself, but I thought the point of actors was to relieve me of some of that burden.