0

I am building a chatting app: I have InboxView and MessagesView, to put it simply both are listening to a @Published variable called chats. When user sends a message in MessagesView, the ranking of inbox changes and because InboxView is listening to updates to chats @Published variable it redraws its views, regardless if the view is hidden! I am assuming this is some SwiftUI eager drawing optimization. Cool.

Now the problem is that InboxView has a List of NavigationLinks, which means during this redraw the NavigationLink somehow resets its state and navigates the user back! Or even worse, pushes the user back to the same chat, again. You can see an example setup here, but what's missing is the data source.

I can't rely on onAppear or onDisappear because they are called as if the user navigates back to the view, so InboxView thinks it's being presented!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • As this depends on views interaction it needs reproducible example. Would you provide code? – Asperi Jun 11 '20 at 04:04
  • A view updates when an observed property changes (marked by `@ObservedObject`). If the list of navigation links does NOT depend on changes to the object containing your `@Published` chats, you can put the views that DO inside a child view. Do NOT use `@ObservedObject` in the InboxView, but rather in the child view. Without [reproducible example code](https://stackoverflow.com/help/minimal-reproducible-example) it is hard to give a better answer. – Jack Goossen Jun 11 '20 at 07:36

0 Answers0