I'm making a chat-like application with a main view called ChatView and a list of Conversation Views with different contacts. The idea behind the app is that messages are split into 3 parts and sent in a form of hyperlinks. The person receiving the message clicks the link and is presented with a sheet with a list of contacts in the app. When choosing a contact, the message is added to Core Data.
I registered an onOpenURL handler on ChatView:
ChatView()
.environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
.onOpenURL { url in
acceptedURL = .init(id: .init(), url: url)
}
.sheet(item: $acceptedURL) { link in
MessageDecryptView(url: link.url)
}
}
Inside MessageDecryptView I do some processing and add the message to Core Data.
The issue I have is that if the app is displaying a conversation with specific user and a hyperlink is opened adding the message to that same conversation, the conversation is not updated when the sheet dismisses. Instead the user has to go back to ChatView and enter the ConversationView again which fetches the messages from Core Data onAppear.
So for the scenario of MessageDecryptView sheet being opened on top of ConversationView I am looking for one of two possible solutions:
- how to go back to
ChatView - (prererable) How to update the displayed
ConversationViewwith a message that was just added
I would appreciate any help!
EDIT
My app is written in SwiftUI. I have ViewModel classes that are used by my views for interacting with Core Data. Inside ConversationView there is
@ObservedObject private var messageVM = MessageViewModel()
and then the following is how I fetch the data from the database:
.onAppear(perform: {
userVM.getCurrentUser()
messageVM.getConversation(with: user.id!)
})