So in swift6 this code I was using is no longer allowed:
@Observable
class ChatViewModel {
var messages: [Message] = []
func loadMessages() {
sdk.loadMessagesInBackground() { messages in
Task { @MainActor in
self.messages = messages
}
}
}
}
The issue being that I cannot move "self" to MainActor. I could declare @MainActor on the whole class but I was trying to see what else might be possible. I did this and it worked:
@Observable
class ChatViewModel {
var messages: [Message] = []
func loadMessages() {
sdk.loadMessagesInBackground() { messages in
var selfMessages = self.messages
Task { @MainActor in
selfMessages.replaceSubrange(0..<selfMessages.count, with: messages)
}
}
}
}
So I'm just curious now how these are different. The actual thread safety issues should be identical between the two of them, but one is allowed. Why?