DataStore イベント
DataStore は定期的に状態通知を Amplify の Hub に発行します。Hub にサブスクライブして、DataStore の内部状態を理解することができます。イベントは以下の場合に発行されます:
- デバイスがネットワーク接続を失うか、または回復する場合
- データがクラウドと同期される場合
- まだ同期されていない保留中の変更がある場合
以下の DataStore イベントが定義されています:
networkStatus
DataStore が起動した時と、ネットワークステータスが変更されるたびに発行されます
HubPayload NetworkStatusEvent には以下が含まれます:
active(Bool):DataStore がクラウドに接続できるネットワーク上にある場合は true、それ以外の場合は false
subscriptionsEstablished
DataStore がすべてのモデルへのサブスクリプションの確立を完了した時に発行されます
HubPayload:N/A
syncQueriesStarted
DataStore が初期同期クエリを実行しようとする時に発行されます
HubPayload syncQueriesStartedEvent には以下が含まれます:
models([String]):各モデルのnameの配列
modelSynced
Dispatched once for each model after the model instances have been synced from the cloud
HubPayload modelSyncedEvent contains:
model:name(String): the name of the model that was synced
isFullSync(Bool):trueif the model was synced with a "full" query to retrieve all modelsisDeltaSync(Bool):trueif the model was synced with a "delta" query to retrieve only changes since the last syncnew(Int): the number of new model instances added to the local storeupdated(Int): the number of existing model instances updated in the local storedeleted(Int): the number of model instances deleted from the local store
syncQueriesReady
すべてのモデルがクラウドから同期されたときに発行されます
HubPayload:N/A
ready
DataStore 全体が準備完了した時に発行されます。この時点ですべてのデータが利用可能です
HubPayload:N/A
outboxMutationEnqueued
Dispatched when a local change has been newly staged for synchronization with the Cloud
HubPayload outboxMutationEvent contains:
model:name(String): the name of the model that is awaiting publication to the Cloud
element:model(Model): the model instance that will be published
outboxStatus
以下の場合に発行されます:
- DataStore が起動する場合
- ローカル変更がアウトボックスにキューイングされるたびに
- ローカル変更の処理が完了するたびに
HubPayload OutboxStatusEvent には以下が含まれます:
isEmpty(Bool):クラウドへのアップロードがまだ保留中のローカル変更がないことを示すブール値
使用方法
ネットワークステータスがアクティブであるかどうかを確認するには、以下のリスナーを設定できます:
// Create listenerconst listener = Hub.listen('datastore', async hubData => { const { event, data } = hubData.payload; if (event === 'networkStatus') { console.log(`User has a network connection: ${data.active}`) }})
// Remove listenerlistener();To wait for the entire sync process to finish, you can listen for the ready event:
// Create listenerconst listener = Hub.listen("datastore", async hubData => { const { event, data } = hubData.payload; if (event === "ready") { // do something here once the data is synced from the cloud }})
// Remove listenerlistener();Here is an illustrative sample of events and payloads that happen when you start from an empty DataStore and start a sync. If you do:
await DataStore.clear();await DataStore.start();This gets logged:
Event: {"channel":"datastore","payload":{"event":"storageSubscribed"},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"networkStatus","data":{"active":true}},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"outboxStatus","data":{"isEmpty":true}},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"subscriptionsEstablished"},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"syncQueriesStarted","data":{"models":["ModelX","ModelY","ModelLala"]}},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"modelSynced","data":{"isFullSync":true,"isDeltaSync":false,"counts":{"new":5,"updated":0,"deleted":2}}},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"modelSynced","data":{"isFullSync":true,"isDeltaSync":false,"counts":{"new":296,"updated":0,"deleted":2}}},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"modelSynced","data":{"isFullSync":true,"isDeltaSync":false,"counts":{"new":8155,"updated":0,"deleted":0}}},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"syncQueriesReady"},"source":"","patternInfo":[]}
Event: {"channel":"datastore","payload":{"event":"ready"},"source":"","patternInfo":[]}