Hello,
I'm a little bit confused about serial execution of actor methods when non-mutable properties within these methods are involved. Imagine following code:
public actor ActorA
{
private let url1: URL // <-- Immutable!
private let url2: URL // <-- Immutable!
// @MainActor is superfluous?
func method1()
{
try! FileManager.default.copyItem(at: url1, to: url2)
}
// @MainActor is superfluous?
func method2()
{
try! FileManager.default.removeItem(at: url1)
}
// @MainActor is superfluous?
func method3()
{
try! FileManager.default.moveItem(at: url2, to: url1)
}
// @MainActor is superfluous?
func method4()
{
try! FileManager.default.removeItem(at: url2)
}
}
Is it ensured that outside access (not from self!) to method*
always happens in a serial fashion in the way that each call is put on some sort of messaging queue? I'm not quite sure if I understand the documentation proposals/0306-actors.md
correctly. Note that only immutable properties of ActorA
are involved!
Basically I want to ensure the these methods never ever run concurrently. Because of that I tagged all methods with @MainActor
but is this really necessary? Or ensures ActorA
itself by its messaging system that calls to its methods happens in a serial way as implied by 0306-actors.md
, even where immutable properties (like url1
, url2
) are involved?
Previously I had an issue where these methods (using FileManager
methods operating on same file) accidentally are called concurrently from different tasks which leads to a total mess as you might imagine.
Thanks in advance for any clarification on this topic or links to previously posts or additional documentation!