On appear Modifier wont trigger listener on first view appearance.
When I sign into the application and click the appropriate tab view it does not trigger the on appear… it only triggers it when I click it twice…It works in all other situations but when I sign in for the first time it does not trigger when I click the Conversationlistview Tab.
ContentView {
TabView(selection: $selectedTab) {
FriendsView(selectedTab: $selectedTab, cameFromConversationsListView: true)
.tabItem {
Label("Friends", systemImage: "message")
}
.tag(Tab.friends)
ConversationsListView(selectedTab: $selectedTab, cameFromConversationsListView: true)
.tabItem {
Label("Messages", systemImage: "person.fill")
}
.tag(Tab.Messages)
.onAppear {
setupInitialListeners()
}
SettingsView()
.tabItem {
Label("Settings", systemImage: "gearshape")
}
.tag(Tab.settings)
}
}
......
struct ConversationsListView: View {
var body: some View {
Vstack{
//OTHER CONTENT TO DISPLAY HERE
}
.onAppear(perform: {
DispatchQueue.main.async {
setupInitialListeners()
}
})
}
}.....
I have tried placing the setupInitialListener in a ViewModel then calling it as ViewModel.setupInitialListener. I placed the on appear on the tabview. I placed it in the sign in function when I sign in to the app initially. I tried calling it from the @main Delegate. I also created a custom tab format with navigation links. I’m not sure what to do.
2
Answers
I got it to work and finally call the listener... strangely enough it only works when I use...
and put the same call for the listener in both functions...when I tried to call it individually in either disappear or appear it wont trigger... But when using both with on disappear above the appear it works. I have no idea why but I played around and tried this and now it does work.. maybe this will help someone in the future. Thanks allot for your responses Jayant. You gave me a reason to keep trying. All the best.
Add the body in your
ContentView
and remove thesetupInitialListeners()
from two places. Either put it inside theConversationsListView
orContentView
, and it will work fine as expected. When you tap onMessages
tab, thesetupInitialListeners()
function will be called when the view appears. Here is my code, which is working fine.and Please provide minimal reproducible code next time