Static property 'shared' is not concurrency-safe because it is non-isolated global shared mutable state

Hi all,

I am fairly new to Swift development so go easy on me!

I am working through a few examples of using Reality Kit content within my projects and whilst trying to work on adding gestures to RealityKit entities, I have come across a weird issue.

Downloading and running the example here

This works fine for me.

When adding the same things to my own code - in this case a class called EntityGestureState to my GestureComponent file (within the reality kit project) I constantly get this error:

"Static property 'shared' is not concurrency-safe because it is non-isolated global shared mutable state"

Even just troubleshooting with something as simple as:

public class EntityGestureState {
    // The entity currently being dragged if a gesture is in progress.
    
    // Singleton shared instance
    static let shared: EntityGestureState = EntityGestureState()
    

}

I immediately get the error and from a bunch of trial and error and reading different sources I can't seem to get around this.

Could anyone help here? I am running on Xcode 16 beta 3 so am wondering if it's a bug but also more than likely user-error.

Answered by Vision Pro Engineer in 799167022

Hey @njsnx,

This is not a bug. This is an error that is triggered when using the Swift 6 Language. Inside your target's Build Settings you can change the Swift Language Version to Swift 5 to temporarily avoid these errors. If you go this route you should enable Complete Swift Concurrency Checking to create warnings for these issues that are errors in Swift 6.

The Sample Code uses Swift 5 which is why this is only an issue in your project. I'd highly encourage you to remain on Swift 6 and fix the underlying errors. We published a WWDC video and a migration guide to help you in the process:

In terms of the underlying changes that are required for the Entity Gestures code I would consider refactoring the EntityGestureState shared instance to be a property on the GestureComponent. This involves changing the EntityGestureState to a Codeable struct which also requires changing some of the properties on the structure. In my own code, I've changed the Entitys to use the IDs of type UInt64. and the initial orientation to use a SIMD4<Float> vector. Additionally, many of the functions inside GestureComponent will need to be attributed with @MainActor.

Let me know if you have any follow up questions,

Michael

Hey @njsnx,

This is not a bug. This is an error that is triggered when using the Swift 6 Language. Inside your target's Build Settings you can change the Swift Language Version to Swift 5 to temporarily avoid these errors. If you go this route you should enable Complete Swift Concurrency Checking to create warnings for these issues that are errors in Swift 6.

The Sample Code uses Swift 5 which is why this is only an issue in your project. I'd highly encourage you to remain on Swift 6 and fix the underlying errors. We published a WWDC video and a migration guide to help you in the process:

In terms of the underlying changes that are required for the Entity Gestures code I would consider refactoring the EntityGestureState shared instance to be a property on the GestureComponent. This involves changing the EntityGestureState to a Codeable struct which also requires changing some of the properties on the structure. In my own code, I've changed the Entitys to use the IDs of type UInt64. and the initial orientation to use a SIMD4<Float> vector. Additionally, many of the functions inside GestureComponent will need to be attributed with @MainActor.

Let me know if you have any follow up questions,

Michael

Really appreciate the extensive answer. I’ll have a go with this later!

I was thinking to approach this as “make it work with swift 6” rather than ignore the errors so thanks for the direction

will see how this goes and report back

Static property 'shared' is not concurrency-safe because it is non-isolated global shared mutable state
 
 
Q