2017-07-22 7 views
0

私は開発しているionic3アプリケーションに問題があります。 私のアプリでJSONサーバーに接続し、たくさんの行をダウンロードします。forを使用してすべての行を取得すると、各行で関数を呼び出してデータを挿入します。 しかし、私の考えは、挿入機能よりも速く、終了前にファイナルアラートを表示することだと思います。これは私のコードです:これはイオン3多くのデータを挿入

public insertRow(indice: any, row1: any, row2: any, row3: any, row4: any, 
    row5: any, row6: any, row7: any, row8: any, row9: any, row10: any, 
    row30: any) 
{  
    let sql = "INSERT INTO TableRows (id,Nombre,Rio,Pais,Comunidad, 
    Zona,Localidad,Interes,Caracter,Cascada_max,Cuerda_max,Desnivel, 
    Longitud, Tiempo_aprox,Tiempo_descenso,Tiempo_retorno,Observaciones, 
    Descripcion_barranco,Periodo_optimo,Caudal,Caudal_filtro,Aproximacion, 
    Retorno,Loc_entrada_barranco,Loc_salida_barranco,Loc_entrada_parking, 
    Loc_salida_parking,Autor,Actualizacion,Idioma,Visible) 
    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
    console.log(sql); 
    return this.isReady() 
    .then(()=>{ 
     return this.database.executeSql(sql, 
      [indice,BARRANCO, RIO, PAIS,COMUNIDAD, 
       ZONA,LOCALIDAD,INTERES,CARACTER, CASCADA_MAX, 
       CUERDA_MAX,DESNIVEL,LONGITUD,TIEMPO_APROX, 
       TIEMPO_DESCENSO,TIEMPO_RETORNO, OBSERVACIONES, 
       DESCRIPCION_BARRANCO,PERIODO_OPTIMO,CAUDAL, 
       CAUDAL_FILTRO,APROXIMACION, RETORNO, 
       LOC_ENTRADA_BARRANCO,LOC_SALIDA_BARRANCO, 
       LOC_ENTRADA_PARKING, LOC_SALIDA_PARKING, 
       AUTOR,actualización,idioma, visible]); 
    }).then((data)=>{ 
    console.log("El insert devuelve esto " + JSON.stringify(data)); 
    })   

} 

誰かが私が時間通りに行うことができます方法を知っているのインサートのサービスです

private downloadRows() 
{ 
this.platform.ready().then(()=>{ 
    this.translateService.get('ACTUALIZANDOBARRANCOS').subscribe(
    value => { 
    let loadingContent = value; 
    let loader = this.loadingController.create({ 
    content: loadingContent, 
    spinner: "bubbles" 
    }); 

    this.servicioDownloads.downloadAllRows().then(rows=> { 
     this.datosrows= rows; 
     loader.present(); 
     for (var i = 0; i < this.datosrows.length; i++) 
     { 
      var datos = this.datosrows[i]; 
      // we are going to insert rows 
      this.servicioDataBase.insertRow 
      (datos.indice, 
      datos.row1, 
      datos.row2, 
      datos.row3, 
      datos.row4, 
      datos.row5, 
      datos.row6, 
      datos.row7, 
      datos.row8, 
      datos.row9, 
      datos.row10, 
      //... 
      datos.row30 
     ).catch(()=>{ 
      console.log("da error"); 
      }); 
     } 
     loader.dismiss(); 
     this.translateService.get('FINALIZADO').subscribe(
       Titulo =>{ 
        let titulo = Titulo; 

       this.translateService.get('BARRANCOSACTUALIZADOS').subscribe(
        Descripcion =>{ 
         let descripcion = Descripcion; 
         let alerta = this.alertCtrl.create({ 
         title: titulo, 
         subTitle: descripcion, 
         buttons: ['OK'] 
         }) 
         alerta.present(); 
        } 
       ); 
       } 
      ); 


    }).catch(error=>{ 
    loader.dismiss(); 
     this.translateService.get('ERROR').subscribe(
       Titulo =>{ 
        let titulo = Titulo; 

    this.translateService.get('ERRORDESCARGABARRANCOS').subscribe(
        Descripcion =>{ 
         let descripcion = Descripcion; 
         let alerta = this.alertCtrl.create({ 
         title: titulo, 
         subTitle: descripcion, 
         buttons: ['OK'] 
         }) 
         alerta.present(); 
        } 
       ); 
       } 
     ); 
    }) 
}); 
}) 

}

//、私は終了のためのときに挿入意味ですか?

ありがとう!

+0

cordova-sqlite-porterを試すことができます。 https://stackoverflow.com/a/35385963/5059916 –

+0

ありがとう@TomislavStankovic、私はそれを見に行くと私はあなたに教えてくれる –

+0

@TomislavStankovicそれは完璧に動作します、ありがとう、私はsqliteポーターを使用し、正常に動作します! 1つだけがもっと考えると思う。すべてのデータをインポートしたときに、すべての行を表示すると画面に出ましたが、近いアプリケーションの後にこの行が表示されなくなり、再び開いてしまいました。私はvirtualscrollを使用します。 –

答えて

0

async/awaitを関数内で使用するだけで、非同期関数を順番に呼び出すことができます(Typescript 2.1では、非同期/ ES3/ES5を待つことができます)。だからあなたの例のソリューションは、次のようになります。

 
private downloadRows() 
{ 
this.platform.ready().then(()=>{ 
    this.translateService.get('ACTUALIZANDOBARRANCOS').subscribe(
    async value => { 
     let loadingContent = value; 
     let loader = this.loadingController.create({ 
      content: loadingContent, 
      spinner: "bubbles" 
     }); 

     let rows = await this.servicioDownloads.downloadAllRows(); 
     loader.present(); 
     for (let datos of rows) { 
      // we are going to insert rows 
      try { 
      await this.servicioDataBase.insertRow 
       (datos.indice, 
       datos.row1, 
       datos.row2, 
       datos.row3, 
       datos.row4, 
       datos.row5, 
       datos.row6, 
       datos.row7, 
       datos.row8, 
       datos.row9, 
       datos.row10, 
       //... 
       datos.row30 
      ); 
      } 
      catch(error) { 
       console.log(error); 
      } 
     } 
     loader.dismiss(); 
     this.translateService.get('FINALIZADO').subscribe(
       Titulo =>{ 
        let titulo = Titulo; 

       this.translateService.get('BARRANCOSACTUALIZADOS').subscribe(
        Descripcion =>{ 
         let descripcion = Descripcion; 
         let alerta = this.alertCtrl.create({ 
         title: titulo, 
         subTitle: descripcion, 
         buttons: ['OK'] 
         }) 
         alerta.present(); 
        } 
       ); 
       } 
      ); 
    }).catch(error=>{ 
    loader.dismiss(); 
     this.translateService.get('ERROR').subscribe(
       Titulo =>{ 
        let titulo = Titulo; 

    this.translateService.get('ERRORDESCARGABARRANCOS').subscribe(
        Descripcion =>{ 
         let descripcion = Descripcion; 
         let alerta = this.alertCtrl.create({ 
         title: titulo, 
         subTitle: descripcion, 
         buttons: ['OK'] 
         }) 
         alerta.present(); 
        } 
       ); 
       } 
     ); 
    }) 
}); 
}) 

awaitサイクルのために、インサートが実際に行われる前に終了するサイクルのために防ぐことができます。

0

SQLite Porter - このコルドバ/ PhoneGapのプラグインは、SQLまたはJSONのいずれかを使用してSQLiteのデータベースへ/からのインポート/エクスポートするために使用することができます。

インストール:

$ ionic cordova plugin add uk.co.workingedge.cordova.plugin.sqliteporter 
$ npm install --save @ionic-native/sqlite-porter 

使用法:

import { SQLitePorter } from '@ionic-native/sqlite-porter'; 


constructor(private sqlitePorter: SQLitePorter) { } 

... 

let db = window.openDatabase('Test', '1.0', 'TestDB', 1 * 1024); 
// or we can use SQLite plugin 
// we will assume that we injected SQLite into this component as sqlite 
this.sqlite.create({ 
    name: 'data.db', 
    location: 'default' 
}) 
    .then((db: any) => { 
    let dbInstance = db._objectInstance; 
    // we can pass db._objectInstance as the database option in all SQLitePorter methods 
    }); 


let sql = 'CREATE TABLE Artist ([Id] PRIMARY KEY, [Title]);' + 
      'INSERT INTO Artist(Id,Title) VALUES ("1","Fred");'; 

this.sqlitePorter.importSqlToDb(db, sql) 
    .then(() => console.log('Imported')) 
    .catch(e => console.error(e)); 

あなたはcordova-sqlite-porterを使用して試すことができます。あなたの挿入物にを使用して JSON構造体を渡します。optimise the insertionをSQLite DBに入れます。

example projectは15,000+レコードの挿入を示します。 サムスンギャラクシーS4では、単一SQLインサート ステートメントの実行には約5分/ 300秒かかりますが、最適化されたJSON 相当(UNION SELECT - see here for infoを使用)は、同じデバイスで3秒(100倍高速)です。 - source

+0

ありがとうたくさんの作品が完璧! –

+0

@トニプチェ私は助けてくれてうれしいです。あなたが望むなら、他人がそれをより簡単に見つけることができるように、それを受け入れられたものとしてマークすることができます。 –

関連する問題