0

I have a DB function as shown below:

If I want to use the results of this query in a page module, where do I call this function? Do I call it in the constructor or the lifecycle method (ionViewDidLoad, ionViewDidEnter). Please advise.

Currently I call it in the constructor but it seems like it slows down the app. I am not sure if I am doing the right thing

 getAddressDetails() {

 return new Promise((resolve, reject) => {

 let sql = "select * from tablename"
 this.database.executeSql(sql, []).then((data) => {

 resolve(data)}
   });
 }```

1 Answers1

0

(...) main difference between the constructor and the ionViewDidLoad (...) is that sometimes you want to interact with the DOM (maybe to initialize a map).

In that case, if you try to access the DOM in the constructor, you will notice that the DOM is not ready by that point and you won't be able to get the map element. The correct approach to do it would be inside the ionViewDidLoad (...).

See this answer.

constructor is called before all, once per instantiation of the page, here you can do initialization that does not refer the HTML DOM

ionViewDidLoad is called when the page DOM has been loaded, before than the page is shown, also a single time per page instantiation, here you can do initialization thet needs the HTML DOM to be ready

And see this answer.

Filipe Manuel
  • 967
  • 2
  • 14
  • 33
  • Got you @Filipe , from what I understand, it is not really wise to do heavy stuff(Loading around 2000 records from the DB) on the constructor itself. Is that right? – Luqmaan Taha Siddiqui Apr 17 '19 at 16:04
  • If you have a lot of records, try pagination. Or use some technique of lazy loading list items. Look [this](http://masteringionic.com/blog/2018-02-13-lazy-loading-images-and-managing-lists-with-ionic-virtualscroll/) article. – Filipe Manuel Apr 17 '19 at 16:53