Which is more efficient: Storing the database as a private member variable in Ionic 3 SQLite or creating a new database for every query

Here's a question for you - in the context of Ionic 3, what would be the preferable approach: keeping the opened database as a private member variable within a database provider class, or calling create every time a query is made to the database?

For example...

private db: SQLiteObject;

constructor() {
  this.sqlite.create(...)
    .then((db: SQLiteObject) => {
      this.db = db;
    })
}

queryMethod() {
  db.executeSql(sql, {});
}

...or perhaps like this?

constructor() {

}

queryMethod() {
  this.sqlite.create(...)
    .then((db: SQLiteObject) => {
      db.executeSql(sql, {});
    });
}

I do recognize a potential issue with the first approach, as there is a chance that the database might not have been created prior to being accessed.

Answer №1

A special requirement must be fulfilled when dealing with a Promise, which involves opting for the 2nd choice all the time. By doing so, potential issues can be avoided. Essentially, you should only execute the query after ensuring that the promise has been resolved.

Always adhere to the following pattern:

queryMethod() {
  this.sqlite.create(...)
  .then((db: SQLiteObject) => {
    db.executeSql(sql, {});
  });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Jasmine attempting to access a nonexistent property

I created a very basic component: import { Component } from '@angular/core'; @Component({ selector: 'loading', templateUrl: './loading.component.html', styleUrls: ['./loading.component.scss'] }) export ...

An object resulting from the combination of two separate objects

After reading a helpful solution on StackOverflow about merging properties of JavaScript objects dynamically, I learned how to utilize the spread operator in Typescript. However, one question still remains unanswered - what will be the type of the object c ...

When creating utility classes, is it beneficial to offer a non-mutable API to facilitate their integration with frameworks such as React?

Currently, I am working on enhancing the functionality of my DateWithoutTime class. As part of this process, private fields within the class need to be updated by public methods. this.state.dateWithoutTimeInstance.shiftBySpecificDaysCount({ daysCount: 5, ...

Angular 5 is unable to access the value of a form control when the name attribute is not specified

Snippet of HTML code: <li class="dropdownfilter" *ngIf="this.arr.inclues('Male')" (click)="getValueGender('Male',1,)" [(ngModel)]="M"><a>Male</a></li> I encountered the following error: ERROR Error: No value a ...

Avoid unnecessary re-rendering of React Native components with each update of the state

I need my component to display either A or B based on the user's proximity to a specific location. I developed a custom hook to determine if the user is nearby. However, I'm facing an issue where the hook constantly returns a new value of true, ...

Error: Invalid character '&' after initializing create-t3-application bootstrap

After initiating a new next.js app with the command npm create t3-app@latest, I encountered an unexpected syntax error when running the app using npm run dev. The error displayed was SyntaxError: Unexpected token '??='. Additionally, my console o ...

How can I ensure that TypeORM, Type GraphQL, Apollo Server, and Azure Functions work together seamlessly?

I have an Azure Function written in TypeScript that utilizes various technologies such as TypeORM, Apollo Server, and TypeGraphQL. The function involves creating resolvers for projects and tasks and establishing a database connection. import { createConne ...

Adding TypeScript definition file to an npm package: A step-by-step guide

Is it possible to include typescript definitions (.d.ts files) in a pure javascript project without using TypeScript itself? I'm struggling to find any information on how to do this directly in the package.json. ...

What is the method for obtaining the union type of interface values (including string enums)?

How can I achieve the following ? Given : enum FooEnum1 { Foo = "foo", }; enum FooEnum2 { Foo = 1, }; interface FooInterface { foo1 : FooEnum1, foo2 : FooEnum2, foo3 : string, foo4 : number, }; I am interested in cre ...

Who is responsible for the addition of this wrapper to my code?

Issue with Sourcemaps in Angular 2 TypeScript App Currently, I am working on an Angular 2 app using TypeScript, and deploying it with the help of SystemJS and Gulp. The problem arises when I try to incorporate sourcemaps. When I use inline sourcemaps, eve ...

Creating a global variable in Angular 4 and Ionic 3 is simple when following these steps

Currently, I am developing an application in ionic3 and within it, there are numerous services and functions that utilize specific variables such as: let apiUrlPublic = 'http://localhost:8080/'; let apiUrl = 'http://localhost:9999/api/&apo ...

Exploring alternatives for navigation in nebular without using the global spinner

I am currently working on customizing the nebular ngrx-admin template. There is a global spinner that shows up when navigating from the header to a new page (component). I want to hide this spinner, specifically for certain components where it's not n ...

Can you explain how this promise functions within the context of the mutation observer, even without an argument?

Recently, I came across a mutation observer in some TypeScript code that has left me puzzled. This particular implementation of a promise within the mutation observer seems unconventional to me: const observer = new MutationObserver((mutations: MutationR ...

What is the best way to position three DIVs next to each other within another DIV while aligning the last DIV to the right?

I need help formatting a simple list item with three DIVs. The first DIV should be left justified, the second should be able to grow as needed, and the third should be right justified. I currently have them stacked side by side, but can't get the last ...

Navigating in an Electron app using Angular

It appears that I am encountering difficulties with routing pages in my angular electron desktop application. Despite configuring the routing similar to an angular app, nothing seems to be functioning properly. I have specified router links such as "/hom ...

Tips on utilizing array filtering in TypeScript by solely relying on index rather than element callback

When running tslint, I encountered the following error message: https://i.sstatic.net/p2W9D.png Is it possible to filter based on array index without utilizing element callback? Any alternative suggestions would be appreciated. ...

Utilizing Angular formControl validators that are interdependent on other formControls

I am currently working on creating a form that includes two dates: dateFrom and dateTo. The validation requirement is that dateFrom must not come after dateTo, and dateTo must not come before dateFrom. To meet this condition, I have set up a form group wi ...

1. "The power of three vows in the world

I encountered an issue while making three HTTP Post requests in my code. The first two requests are successful, and upon debugging the code, they return the correct values. However, the third request returns undefined. The reason behind making these three ...

"Learn the process of distinguishing between a drop-down value and a selected value within a drop-down menu using angular2-multiselect

I need assistance with a requirement involving a multi-select drop-down menu where the values are in the format of "Code - Name". When an option is selected from the drop-down, only the "Code" should be displayed. I am currently using angular2-multiselec ...

Tips for showing the output of the avg function from MySQL in an Angular application

Upon running a query, I retrieved the following data in Json format stored in myDocData: data: [ RowDataPacket { updatedAt: 2020-01-03T18:30:00.000Z, email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail ...