Ionic lifecycle

I’m using SQLite to store user settings,but when I would like to set up these settings (when user starts the app), I just don’t know where to do.
I have the controller and within the $ionicPlatform.ready(function() {…}) with the saving function, but I don’t know where should be the load.

If I understand well, I think the best way is to do it like this : () {
_db = new PouchDB(‘name’, { adapter: ‘websql’ });

In a run of an angular module.

Gensollen is correct that this can go in your .run(function.

I would create a service, say User for example. In here you can create a function that is called init. What init will do is first check if you have any saved settings. If not, then it returns undefined. If so, it can load up those settings and perform an action. (Make sure you return this function at the end of your service so you can use it outside that file).

Then in your .run(function in app.js, you can inject that service and do something like User.init(). Which will run your init function you just built.

Then in user, you can create another function called save, which will save the user settings. That way, next time the app is ran, your User function will check for saved settings, it will find those and load those.

One example:

I save some user info. When I start the app, I have a service which has a function called init. Init will check for the user info, and if I have it, I know I don’t need to log in the user again, I can just load up their info.

Let me know if you need any more help :slight_smile:

Hi DaDanny,

Thank you so much, that helped me a lot! It’s working like a charm.


No problem, glad I could help.