データストレージ
Ionicアプリ内にデータを保存するためのさまざまなオプションを用意しています。
Ionicオフラインストレージ
Ionic Offline Storage は、iOSとAndroidで利用できるクロスプラットフォームなデータストレージシステムです。 安全な(デバイスで暗号化された)Ionicアプリにオフラインストレージを簡単に追加できます。 パフォーマンスが高く、高度なNoSQLデータクエリを提供します。
複雑でデータ駆動型のアプリ、機密データを管理するアプリ、信頼性の低いセルサービス/インターネット接続のエリアで使用するアプリを開発するチームのために作られました。興味あるなら、こちらからはじめてください。
Ionic Storage
Ionic Storageは無料で使える、独立系の開発者や学生、ホビイスト向けのオープンソースです。 キー/値ペアとJSONオブジェクトを保存する簡単な方法を提供します。
さまざまなストレージエンジンを使用し、プラットフォームに応じて 最適なものを選択します。
- ネイティブアプリのコンテキストで実行する場合、StorageはSQLiteを優先的に使用します。 これは、SQLiteが最も安定し、広く使用されているファイルベースのデータベースの1つであり、 OSがそのようなものを排除することを決定するようなlocalstorageやIndexedDBのような いくつかの落とし穴を避けるためです。
- WebまたはProgressive Web Appとして実行されている場合、StorageはIndexedDB、WebSQL、 およびlocalstorageをこの順序で使用しようとします。
Usage
もしSQLiteを使いたいときは、まずcordova-sqlite-storage プラグインをインストールします:
ionic cordova plugin add cordova-sqlite-storage
次に、パッケージをインストールします:
npm install --save @ionic/storage
そして、以下をあなたの
NgModule
デコレーションに追加します(例えば、
src/app/app.module.ts
に追記ください):
import { IonicStorageModule } from '@ionic/storage';
@NgModule({
declarations: [
// ...
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
IonicStorageModule.forRoot()
],
bootstrap: [IonicApp],
entryComponents: [
// ...
],
providers: [
// ...
]
})
export class AppModule {}
最後に、あなたのComponentsやpageに注入します:
import { Storage } from '@ionic/storage';
export class MyApp {
constructor(private storage: Storage) { }
...
// set a key/value
storage.set('name', 'Max');
// Or to get a key/value pair
storage.get('age').then((val) => {
console.log('Your age is', val);
});
}
Storage の設定
Storageエンジンは、特定のストレージエンジンの優先順位、 localForageに渡す任意の設定の両方で構成されます。 localForageの設定ドキュメントについては、こちらをご覧ください: https://github.com/localForage/localForage#configuration
Note: どのカスタム設定も、デフォルト設定とマージされます。
import { IonicStorageModule } from '@ionic/storage';
@NgModule({
declarations: [...],
imports: [
IonicStorageModule.forRoot({
name: '__mydb',
driverOrder: ['indexeddb', 'sqlite', 'websql']
})
],
bootstrap: [...],
entryComponents: [...],
providers: [...]
})
export class AppModule { }
Instance Members
constructor
LocalForageに渡すドライバの順序とその他の設定オプションを使用して、 新しいStorageインスタンスを作成します。
利用可能なドライバオプション: ['sqlite', 'indexeddb', 'websql', 'localstorage'] そして、これはデフォルトの優先順です。
driver
使用されているドライバの名前を取得します。
clear()
保存されているすべてのデータを削除します。 WARNING: HOT!
Returns: Returns a promise that resolves when the store is cleared
forEach()
key, valueの値を順に取得します。
Returns: Returns a promise that resolves when the iteration has finished.
Parameters
iteratorCallback
- a callback of the form (value, key, iterationNumber)
get()
keyを引数にいれることによって、valueを取得します。
Returns: Returns a promise with the value of the given key
Parameters
key
Type:string
- the key to identify this value
keys()
Returns: Returns a promise that resolves with the keys in the store.
length()
Returns: Returns a promise that resolves with the number of keys stored.
ready()
storeが利用可能になったことを反映します。
Returns: Returns a promise that resolves when the store is ready
remove()
keyを引数にわたすことで、valueを削除します。
Returns: Returns a promise that resolves when the value is removed
Parameters
key
Type:string
- the key to identify this value
set()
keyを引数にわたすことでvalueを保存します。
Returns: Returns a promise that resolves when the key and value are set
Parameters
key
Type:string
- the key to identify this value
value
Type:any
- the value for this key