Ionic 7: App life cycle on ios

Hello,

I have a problem with my ionic 7 app on iOS.

Sometimes, when I have it in memory and I go to use other apps, when I return the app does not work correctly.

It seems that the system takes it out of memory to optimize resources and when it tries to recover it, it does not have the information of the view in which it was.

I understand that for these cases, I should save the state of the app, but I don’t know where to save/recover that state or how to save it.

Any ideas?

Greetings

I use Ionic Storage or you could use SQLite.

On app load, my app loads all required data into my store in memory. As the user interacts with the app, the store is updated which then syncs to Ionic Storage. If on app load the store is empty (either a new user or the OS decides to clear IndexedDB) , data is pulled from our backend API.

You’ll need to provide more information if you are looking for a more specific answer.

Hello, thanks for answering

I also use some internal storage with Storage.

But what’s happening to me is very strange, and maybe it may have to do with navigation… but I’m not sure.

In some cases, when I navigate to a page, I make use of passing parameters through the Router, for example:

this.router.getCurrentNavigation().extras.state[“item”]

I don’t know if maybe when the system takes the app out of memory it can’t recover that information, and I should store it before the system needs those resources…

I have also gotten errors when displaying a waiting message, which is generated in a service depending on the language (@ngx-translate/core)

example:

this.translateService.get(‘LOADING’)

This, when I get this error that I described, it shows me “[object Object]”

It’s very strange… because it only happens to me on iOS and randomly. Sometimes when you reload the app, it shows it fine, other times it restarts the app and goes directly to the first view and on certain occasions this happens, it shows the same view where it left off, but it lacks information

Greetings

I do not know if I explained well.

I am in a view, in my app.

I press the home button on the iPhone. I open the browser, several more apps, and then tap my app again.

At that time, only occasionally, the app becomes unusable and displays strange values.

I don’t know what method is executed when the system decides to remove all memory from the app and what lifecycle method is executed when it comes back

Since it is a random error, it is being difficult for me to debug

You could look at appStateChange, pause, and resume from the Capacitor App plugin. They should fire anytime the app changes state (going to background or coming to the foreground). They don’t fire if it’s a fresh boot of the app due to the OS killing it. In that case, you have to handle it as if the app is opening for the first time.

I use appStateChange and it seems to fire every time the app goes to the background and comes back to the foreground. I use it to pause/resume websocket connections and to refresh some data.

The app is in Cordova, do you know if there is something similar?

Thank you so much

Thanks for the link, I was looking at it.

The project is inherited and browsing the code a little and trying to debug I realized a few things.

1 - Use Platform, from ionic-angular (“this.platform.resume.subscribe”) to detect that the platform is ready. The strange thing that it seems to go off several times in a row
2 - The ngOnInit of AppComponent is also executed twice as soon as the app is started.
3 - Debugging with xcode and the ‘Devices and Simulators’ window, I have managed to force the app to be removed from memory and here I also saw a very strange thing… with the app in the background, supposedly out of memory because the system removed it , the constructor and ngOnInit of the view in which the app was was executed…

I don’t know why he behaves like this…