I am testing out the new FaceTime API's for Shared activities. Even though I followed along with the developer sessions, my data will occasionally get out of sync. I belive it may have something to do with the runtime error I am getting below.
"Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates." Line
The developer sessions show the sending and receiving call's to the API as asynchronous.
@Published var groupSession: GroupSession<DrawTogether>?
var messenger: GroupSessionMessenger?
func configureGroupSession(_ groupSession: GroupSession<DrawTogether>){
reset()
let messenger = GroupSessionMessenger(session: groupSession)
self.messenger = messenger
self.groupSession = groupSession
groupSession.$activeParticipants
.sink { activeParticipants in
let newParticipants =
activeParticipants.subtracting(groupSession.activeParticipants)
async {
do {
try await messenger.send(CanvasMessage(elements: self.elements), to: .only(newParticipants))
} catch {
print("Unable to update new participants")
}
}
}
.store(in: &subscriptions)
let emojiTask = detach { [weak self] in
for await (message, _) in messenger.messages(of:
UpsertEmojiMessage.self){
await self?.handle(message)
}
}
tasks.insert(emojiTask)
let task = detach { [weak self] in
for await (message, _) in messenger.messages(of: CanvasMessage.self) {
await self?.handle(message)
}
}
tasks.insert(task)
groupSession.join()
}
func handle(_ message: UpsertEmojiMessage) {
let element = Element(emoji: message.emoji, postion: message.position)
elements.append(element)
}
func handle(_ message: CanvasMessage) {
//guard message.elements.count > self.elements.count else { return }
self.elements = message.elements // Warning occurs on this line
}