79404379

Date: 2025-02-01 03:40:37
Score: 2
Natty:
Report link

@preconcurrency can only force the struct type to conform Sendable to silence the error, but your Foo2 is a class. And when you try to call the class across the different Tasks(actor) that will cause the data race like your first example. Your second example is ok because you add the @MainActor in the Task closure that make your class still stay in the same Task (which is @MainActor).

To force the class type to conform Sendable, try:

extension Foo2: @unchecked Sendable {}
Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @MainActor
  • Starts with a question (0.5): can on
  • Low reputation (1):
Posted by: Tyler