Ionic 4 Tab to page then back to Tab did not trigger ionViewWillEnter [Solved]

Not that anybody need care about my opinion, but for what it’s worth, I think lifecycle events should not be (ab)used to solve the general problem of “displaying stale data”. What people tend to do:

interface Profile {
  name: string;
 // ...
}
class ProfileService {
  profile: Profile;
}
class ProfileDisplayPage {
  profile: Profile;
  constructor(private profiler: ProfileService) {}
  ngOnInit(): void { this.profile = this.profiler.profile; }
}
class ProfileEditPage {
  profile: Profile;
  constructor(private profiler: ProfileService) {}
  updateProfile(): void { this.profiler.profile = this.profile; }
} 

…then they ask “how do I force reloading of ProfileDisplayPage?”

What I do is to leverage RxJS to solve the problem of “how do I ensure that updates propagate easily and efficiently across my app?”:

class ProfileService {
  profile$ = new BehaviorSubject<Profile | null>(null);
  watchProfile(): Observable<Profile | null> { return this.profile$; }
  peekProfile(): Profile | null { return this.profile$.value; }
  pokeProfile(profile: Profile): void { this.profile$.next(profile); }
}
class ProfileDisplayPage {
  profile$: Observable<Profile | null>;
  constructor(private profiler: ProfileService) { this.profile$ = profiler.watchProfile(); }
}
class ProfileEditPage {
  profile: Profile;
  constructor(private profiler: ProfileService) { this.profile = profiler.peekProfile(); }
  updateProfile(): void { this.profiler.pokeProfile(this.profile); }
}

There are a myriad of options for accessing Observables from a template. Many of them involve memory leaks or overly verbose boilerplate. Two that don’t are the AsyncPipe and NetanelBasal’s ngx-take-until-destroy.

I frequently think in terms of “WORM (Write Once / Read Multiple) brokers”, where a resource has many readers and a single writer. The broker in this case is the ProfileService, which is injected by anybody interested in the Profile resource. It exposes three operations: watch, peek, and poke. Readers who want updates want watch. Transient readers that don’t care about updates can use peek. Writers generally take a snapshot with peek, modify it, and push the updates back with poke.

All of this happens with no additional code on the part of readers, no dependencies on the vagaries of framework internals (such as what lifecycle events fire when on what UI components), and no wasteful performance hits (such as you see with “solutions” to this problem that completely rerender a bunch of irrelevant stuff, often via action-at-a-distance).

It is also easy (and transparent) to add network and/or backing on-device storage to ProfileService. Nobody outside that class has to know or care when or how external forces collude to bring fresh data.

26 Likes
The Events provider is deprecated - ionic 4
Ionic Refresh Current Page
Reload current page / ionViewWillEnter [solved]
How to reload app.component on event in ionic 4
Observables - Unsubscribing httpClient required?
[Ionic 4] Reuse same previously fetched data
Ionic 4 refresh tab
Life cycle is not working
Ionic 4 page reload after back button click
How Do We Detect Component Initialization? [SOLVED]
Ionic 4 - search with service does not update page data after new data
Open login page and go back with refresh
Update ionic component view across app through settings page
How to select list in ionic 4 from firebase and display it into another page
IONIC 4 -> Back Button - ngonInit or ionViewWillEnter never fired
Can I use @Input and @Output on my tab pages?
Ionic 4 Global variable for the application
HTML element don't show values of a provider
Page reload on push
Going from Ionic Events to Observables
Going from Ionic Events to Observables
Unable to call ion will remove /ng on destroy using ionic 5
How do make ionic app detect change when update is made?
How to fire ngOnInit on page
Data does not update second time
Ionviewwillenter does not auto refresh
Ionic 5 - Reset data from ionic-selectable for show different details
Clear Ion Router Outlet Programmatically
How to Sync ionic tab data with API data when tab is clicked
Passing data back from nav.pop()
Finally made a breakthrough passing data to a component - felt I should share
Save QueryParams while switching in between tabs
Static Components - Events
Having issue showing data on html page
Ionic 5 capacitor : Load the data of a page only once even if the page is loaded 1000 times
BehaviorSubject problem
IonViewDidEnter not firing on stacked navigation
Observable only returns cached data
Can i call LifeCycle methods in Refresh function
Update content based on side menu ionic 4
routerLink not updating url value when changed
Making component to wait until async operations in the provider constructor gets resolved
How to refresh a page after login.?
FormSubmit doesn't update value without refresh page
Ionic 4 menu with user role based authentication
Where to securely store custom access rights for a user? Cookies / Local Storage / somewhere else?
Ionic 4 not showing selected value in ion-select
Creating a running total from an array of data
iOS sometimes loses angular variables when resuming from the background
How to remove white screen after window.location.reload(); in ionic 4
Remove cached data, when i revisit the same screen again
Update Parent Tabs on child page exit
Ionic router push same page
Having tough time with the Observer pattern
How to call a service function in the app.component.ts file?
Ionic Page and component relationship
Reset app on logout. Pop pages from nav stack and delete services data
How do I know if a component is viewed
How do I know if a component is viewed
Make constant value accessible public
Angular variable is not updating when I return to previous page
Change page based on user logged in at the base url
Ion Toggle is not saved - Storage (ionic)
What's the best way to save data from a form and access it from multiple pages in a mobile app?
Pass data from html.page to .ts file
ionViewWillEnter & cross-tab navigation
About how to triggered an event from a page to another
Util events in Ionic 4 and 5?
JWT + LocalStorage + Behaviour Subject + Page information dosen´t load refresh the new data
Doubth with navigate Ionic V5
What's the best way to save data from a form and access it from multiple pages in a mobile app?
Capacitor tab page is not refreshing on tab change and if come back on that page
Ionic 4 change local storage value
How to pass data to appcomponent page from other page
Return an Observable by using a Storage value + Good practices advices
How to properly re-render a component in Ionic manually
Do I understand the routing wrongly?
Communicating between two components in a page - Ionic Angular components
Tab menu refresh ionic angular
When does ngOnInit run? Serious question
Profile Page
Can `ion-menu` pop a page instead of pushing a page?
Refresh on back button
Back button issue. Previous page is not triggerring any methods
Handle tabs caching and logout
Why does Ionic 4 provide NavParams either it does not work?
Best way to replace deprecated Events
Displaying dynamic API results in a span