<BlockSwitcher>

<Block name="Async/Await">

```swift
// You must hold a reference to your subscription.
var postsSubscription:  AmplifyAsyncThrowingSequence<MutationEvent>?

// Then in the body of your code, subscribe to the subscription
func subscribeToPosts() async {
    let postsSubscription = Amplify.DataStore.observe(Post.self)
    self.postsSubscription = postsSubscription

    do {
        for try await changes in postsSubscription {
            // handle incoming changes
            print("Subscription received mutation: \(changes)")
        }
    } catch {
        print("Subscription received error: \(error)")
    }
}

// Then, when you're finished observing, cancel the subscription
func unsubscribeFromPosts() {
    postsSubscription?.cancel()
}
```

</Block>

<Block name="Combine">

```swift
// In your type declaration, declare a cancellable to hold onto the subscription
var postsSubscription: AnyCancellable?

// Then in the body of your code, subscribe to the publisher
func subscribeToPosts() {
    postsSubscription = Amplify.Publisher.create(Amplify.DataStore.observe(Post.self))
    .sink {
        if case let .failure(error) = $0 {
            print("Subscription received error - \(error)")
        }
    }
    receiveValue: { changes in
        // handle incoming changes
        print("Subscription received mutation: \(changes)")
    }
}

// Then, when you're finished observing, cancel the subscription
func unsubscribeFromPosts() {
    postsSubscription?.cancel()
}
```

</Block>

</BlockSwitcher>

<Callout>

`DataStore.clear()` and `DataStore.stop()` will stop the DataStore sync engine and keep any subscriptions connected. There will not be any additional subscription events received by the subscriber until DataStore is started (`DataStore.start()`) or the sync engine is re-initiated upon performing a DataStore operation (query/save/delete).

</Callout>

<Callout>

This API is built on top of the [Combine framework](https://developer.apple.com/documentation/combine); therefore, it is only available on iOS 13 or higher.

The `Amplify.Publisher.create` API returns a standard [AnyPublisher](https://developer.apple.com/documentation/combine/anypublisher).

</Callout>