Synchronous query in Sqlite using Ionic

Asked

Viewed 434 times

2

I have the following function below:

public requisicaoXPTA() {
  this.database.executeSql("SELECT * FROM tbl_xpta",
      []).then((data) => {
      // o resultado retorna aqu na variável data

      }, (error) => {
        console.log(error);    
  });
}

When I use this function I do this way:

this.requisicaoXPTA();
this.atualizar();

Most of the time, the function atualizar() is executed before the requisicaoXPTA(). I’d like to perform the function atualizar() after having made the request in the table. What better way to do this synchronously?

  • 2

    Ever tried to use async and await?

  • 1

    A kick, how about adding the this.atualizar within the .then?

  • @Guillhermenascimento if he wears this.atualizar within the .then works, but I would like to add the requisicaoXPTA in another file (class), other than where the this.atualizar if found. So for this reason I wanted to first receive the result of the function and then proceed to update.

  • @Diegoaugusto did not try no. I will take a look to see how it works there. Thanks to the tip.

  • @acklay I will put an example in the answers.

3 answers

1

You can pass a callback to be executed after the request, like this:

public requisicaoXPTA(callback) {
  this.database.executeSql("SELECT * FROM tbl_xpta", []).then((data) => {
    callback();
  }, (error) => {
    console.log(error);    
  });
}

The use would look like this

this.requisicaoXPTA(this.atualizar);

1

I could use the idea of @LINQ, but made the opitional callback:

public requisicaoXPTA(callback) {
  this.database.executeSql("SELECT * FROM tbl_xpta", []).then((data) => {
    if (callback) callback(data);
  }, (error) => {
    console.log(error);    
  });
}

The moment you need the this.atualiza would wear so:

requisicaoXPTA(this.atualiza);

When not using just omit:

requisicaoXPTA();

Or you could use a parameter to tell when to update or not to update, for example:

public requisicaoXPTA(refresh) {

  this.database.executeSql("SELECT * FROM tbl_xpta", []).then((data) => {
    if (refresh) this.atualiza(data);
  }, (error) => {
    console.log(error);    
  });
}

I don’t remember if this works within the => {} due to the scope, if not do so:

public requisicaoXPTA(refresh) {
  var callback = refresh ? this.atualiza : false;

  this.database.executeSql("SELECT * FROM tbl_xpta", []).then((data) => {
    if (callback) callback();
  }, (error) => {
    console.log(error);    
  });
}
  • Oops! Blz bro?! But what I’m saying is that method requisicaoXPTA and updates are in different classes. That’s why I said I didn’t want to use the method this.atualiza within the requisicaoXPTA. In practice the requisicaoXPTA should return me a list of "values". Something in this sense: let values = this.requisicaoXPTA(); So I can use this one values inside this.atualiza();

  • @acklay edited the code, see if it makes sense to you now, I passed date as argument

  • Your first example is making more sense to me. Tomorrow I’ll test and tell you. I’m just not sure if I put in sequence the method atualiza will continue to be executed before the method requisicaoXPTA. You think you can handle it then?!

  • The main problem would be because the method executeSql is asynchronous. Hence the difficulty.

  • if I use only let values = this.database.executeSql("SELECT * FROM tbl_xpta", []); works in parts, but cannot abstract the object data on the return in this way. =/

  • Anyway, tomorrow I’ll do some tests and tell you. VLW

  • @But asynchronous acklay is gorgeous, asynchronous is life, asynchronous provides a healthier UX :)

  • I know, but it doesn’t solve my problem now! heheh... I think I’m doing something wrong. ehuheuhe

Show 3 more comments

0

You can use the async and await which is present since the typeScript 2.1.

Example:

public async requisicaoXPTA() {
    return new Promise((resolve, reject) => {
      this.database.executeSql("SELECT * FROM tbl_xpta",
        []).then((data) => {
          resolve(data);
        }, (error) => {
          reject(error);
        });
    })
}

Note that I am solving the Promise so that we can get the result out of it. Done this you can call your function:

let resultado = await this.requisicaoXPTA();

With the await promisse will be solved and cursor will be locked in this function until it is complete. Then you can call your other function:

let resultado = await this.requisicaoXPTA();
console.log(resultado);
this.atualizar();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.