Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 30, 2024

Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. New project should use Amplify Gen 2. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade. Switch to the latest Gen 2 docs →

DataStore イベント

Amplify iOS v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Swift to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

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:

  • modelName (String): the name of the model that was synced
  • isFullSync (Bool): true if the model was synced with a "full" query to retrieve all models
  • isDeltaSync (Bool): true if the model was synced with a "delta" query to retrieve only changes since the last sync
  • added (Int): the number of new model instances added to the local store
  • updated (Int): the number of existing model instances updated in the local store
  • deleted (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:

  • modelName (String): the name of the model that is awaiting publication to the Cloud
  • element:
    • model (Model): the model instance that will be published

outboxMutationProcessed

Dispatched when a local change has finished synchronization with the Cloud and is updated locally

HubPayload outboxMutationEvent contains:

  • modelName (String): the name of the model that has finished processing
  • element:
    • model (Model): the model instance that is processed
    • _version (Int): version of the model instance
    • _lastChangedAt (Int): last change time of model instance (unix time)
    • _deleted (Bool): true if the model instance has been deleted in Cloud

outboxStatus

以下の場合に発行されます:

  • DataStore が起動する場合
  • ローカル変更がアウトボックスにキューイングされるたびに
  • ローカル変更の処理が完了するたびに

HubPayload OutboxStatusEvent には以下が含まれます:

  • isEmpty (Bool):クラウドへのアップロードがまだ保留中のローカル変更がないことを示すブール値

使用方法

ネットワークステータスがアクティブであるかどうかを確認するには、以下のリスナーを設定できます:

let hubEventListener = Amplify.Hub.listen(to: .dataStore) { event in
if event.eventName == HubPayload.EventName.DataStore.networkStatus {
guard let networkStatus = event.data as? NetworkStatusEvent else {
print("Failed to cast data as NetworkStatusEvent")
return
}
print("User receives a network connection status: \(networkStatus.active)")
}
}
let hubEventSubscriber = Amplify.Hub.publisher(for: .dataStore).sink { event in
if event.eventName == HubPayload.EventName.DataStore.networkStatus {
guard let networkStatus = event.data as? NetworkStatusEvent else {
print("Failed to cast data as NetworkStatusEvent")
return
}
print("User receives a network connection status: \(networkStatus.active)")
}
}

An initial networkStatus event is always dispatched, in which active is set to false. Shortly thereafter, you will receive an updated event that reflects the true status of the network connectivity.

You may want to setup your Hub Listener or Subscriber before calling Amplify.configure(), otherwise you may miss some of the DataStore events that are emitted.