I would like to come back to that topic, as Swift 6 is now out and I would like to use it:
The code with the @globalActor works, but it brings another problem:
@globalActor
actor MyActor {
static let shared = MyActor()
}
@MyActor
@Observable
class MyModel {
static let shared = MyModel()
var text = ""
}
struct ContentView: View {
var body: some View {
VStack {
Text(MyModel.shared.text)
}
.padding()
}
}
This will lead to the error: Global actor 'MyActor'-isolated property 'text' can not be referenced from the main actor.
And there is another problem with another example:
@globalActor
actor MyActor {
static let shared = MyActor()
}
@MyActor
@Observable
class MyModel {
static let shared = MyModel()
var users = [User(name: "Steve", addressId: 0)]
var addresses = [Address(id: 0, street: "Main Street")]
}
struct User {
var name: String
var addressId: Int
}
struct Address {
var id: Int
var street: String
}
extension User {
var address: Address? {
get async {
await MyModel.shared.addresses.first(where: { $0.id == self.addressId })
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Text(MyModel.shared.users.first!.address!.street)
}
.padding()
}
}
Despite the first called problem, I would need to access the address property with await, what doesn't work in the SwiftUI-view.
Any thoughts or ideas?