I have a simple question - would it be better in Ionic 3 to have the opened database in a private member variable of a database provider class, or to call create
every time the database is queried?
I.e. this...
private db: SQLiteObject;
constructor() {
this.sqlite.create(...)
.then((db: SQLiteObject) => {
this.db = db;
})
}
queryMethod() {
db.executeSql(sql, {});
}
...or this?
constructor() {
}
queryMethod() {
this.sqlite.create(...)
.then((db: SQLiteObject) => {
db.executeSql(sql, {});
});
}
I do see a problem with the first approach, as there's a small probability that the database might not have been created before it's accessed.
Yes, Since this is Promise
you always need to use the 2nd
option.Then you will not have any issues. That means you need to execute the query after resolving the promise.
Follow below pattern always:
queryMethod() {
this.sqlite.create(...)
.then((db: SQLiteObject) => {
db.executeSql(sql, {});
});
}