[Ionic 4] How to reload page after navigating

To be clear: It’s not like you have to necessarily unsubscribe for each event. If you want to keep using the event just keep it running. My warning was more to you make you aware of the running ‘background processes’ and it’s potential dangers. I’ve seen many examples in programming (general) where some subscribe to an event and forget about it. They continue using the application and under the hood the app is subscribing like 1000 times (e.g.) for the same event. This means the user is ending up processing the same event for 1000 times(!). This attempt of processing the bulk of ‘events’ might be a hard task for some devices. It may slow down your application and/or even end up crashing your application. In this case: unloading/leaving a ‘view’ is not cleaning up for you the running events and/or subscriptions.

As far as I’ve understand you have a page displaying some data from which you can navigate to another page to edit/update that data. After updating you can navigate ‘back’ and you see the ‘updated’ data?

I’m not sure why your navigation isn’t working properly, but I’ll try to help you out with the situation just as it is. In this example you can keep updating your data and the other view will keep processing the updated data. When navigating back, you unsubscribe for both events.

// data view page
private goToEdit() {
    this.events.subscribe('user:updated', (updatedData) => {
        // do something when updated data
        this.foo = updatedData;
    });

    // event subscription to keep track of your 'state'. Here you'll 'unsubscribe' when done
    this.events.subscribe('returnedFromEdit', () => {
        this.events.unsubscribe('user:updated');
        this.events.unsubscribe('returnedFromEdit');
    });

    // ..navigate to edit page logic..
}

............

// edit page
private updateData() {
    // some processing

    this.events.publish('user:updated', yourUpdatedData);
}

private navigateBack() {{
    this.events.publish('returnedFromEdit'); // naming could be better :-)

    // ..navigate back logic..
}

Good luck!

Btw: If someone has some other suggestions/ideas, I’d like to hear them too!

3 Likes