1

I have this animation function in Swift6 RealityKit application. It puts given entity softly in scene. It works.

But I am getting Capture of 'simpleMaterial' with non-sendable type 'SimpleMaterial' in a '@Sendable' closure warning. How can we get rid of it.

 func fadeInText(entity: ModelEntity, duration: TimeInterval = 1) {
 
 guard var simpleMaterial = entity.model?.materials.first as? SimpleMaterial else { return }
 let steps = 60
 let interval = duration / Double(steps)
 
 
 let startTime = CACurrentMediaTime()
 Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { t in
 let elapsed = CACurrentMediaTime() - startTime
 let progress = min(elapsed / duration, 1.0)
 let alpha = progress
 Task { @MainActor in
 simpleMaterial.color.tint = simpleMaterial.color.tint.withAlphaComponent(CGFloat(alpha))
 
 entity.model?.materials = [simpleMaterial]
 }
 if progress >= 1.0 {
 t.invalidate()
 }
 }
}
asked Jul 11, 2025 at 9:48
2
  • when "guard var simpleMaterial = entity.model?.materials.first as? SimpleMaterial else { return }" placed in Task warning disappears but is it OK? Commented Jul 11, 2025 at 10:19
  • Yes, it's OK. But why do you have a Task here at all? Commented Jul 11, 2025 at 12:38

1 Answer 1

0

when "guard var simpleMaterial = entity.model?.materials.first as? SimpleMaterial else { return }" placed in Task warning disappears but is it OK?

Yes, it's a good workaround. But the problem here is really that the line

Task { @MainActor in

is unnecessary. You don't need a Task here. I presume you are doing this because you want to ensure that the code in the inner closure executes on the main actor. But there is no need for this, because when your timer fires, the entire timer closure will be called on the main actor anyway, so you don't need to switch to the main actor.

If the compiler doesn't realize that the closure will be called on the main actor, just tell it that it will be; instead of saying

Task { @MainActor in

say

MainActor.assumeIsolated {

Personally, I would wrap the entire timer closure in this structure:

Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { t in
 MainActor.assumeIsolated {
answered Jul 11, 2025 at 14:44
Sign up to request clarification or add additional context in comments.

2 Comments

Well compiler gives "Main actor-isolated property 'model' can not be mutated from a Sendable closure" error if we remove Task { @MainActor in ... }
Xcode excepts "MainActor.assumeIsolated {...}" too I did not test it while app runs though, at least there is no compiler error. This is the first app I try to use Swift6. It is rough :(

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.