Page updated Jan 16, 2024

Set up Amplify DataStore

DataStore with Amplify

Amplify DataStore provides a programming model for leveraging shared and distributed data without writing additional code for offline and online scenarios, which makes working with distributed, cross-user data just as simple as working with local-only data.

Note: this allows you to start persisting data locally to your device with DataStore, even without an AWS account.

Goal

To setup and configure your application with Amplify DataStore and use it to persist data locally on a device.

Prerequisites

An application with Amplify libraries integrated and a minimum target of any of the following:

  • iOS 13.0, using Xcode 14.1 or later.
  • macOS 10.15, using Xcode 14.1 or later.
  • tvOS 13.0, using Xcode 14.3 or later.
  • watchOS 9.0, using Xcode 14.3 or later.
  • visionOS 1.0, using Xcode 15 beta 2 or later. (Preview support - see below for more details.)

For a full example, please follow the project setup walkthrough.

visionOS support is currently in preview and can be used by targeting the visionos-preview branch. As new Xcode 15 beta versions are released, the branch will be updated with any necessary fixes on a best effort basis.

For more information on how to use the visionos-preview branch, see Platform Support.

Install Amplify Libraries

  1. To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....

  2. Enter the Amplify Library for Swift GitHub repo URL (https://github.com/aws-amplify/amplify-swift) into the search bar and click Add Package.

Note: Up to Next Major Version should be selected from the Dependency Rule dropdown.

  1. Lastly, choose AWSDataStorePlugin and Amplify. Then click Add Package.

Setup local development environment

To use Amplify, you must first initialize it for use in your project. If you haven't already done so, run this command:

1amplify init

The base structure for a DataStore app is created by adding a new GraphQL API to your app.

1# For new APIs
2amplify add api
3
4# For existing APIs
5amplify update api

The CLI will prompt you to configure your API. Select GraphQL as the API type and reply to the questions as shown below. Conflict detection is required when using DataStore to sync data with the cloud.

1? Please select from one of the below mentioned services:
2 `GraphQL`
3? Here is the GraphQL API that we will create. Select a setting to edit or continue:
4 `Name`
5? Provide API name:
6 `BlogAppApi`
7? Here is the GraphQL API that we will create. Select a setting to edit or continue:
8 `Authorization modes: API key (default, expiration time: 7 days from now)`
9? Choose the default authorization type for the API
10 `API key`
11? Enter a description for the API key:
12 `BlogAPIKey`
13? After how many days from now the API key should expire (1-365):
14 `365`
15? Configure additional auth types?
16 `No`
17? Here is the GraphQL API that we will create. Select a setting to edit or continue:
18 `Conflict detection (required for DataStore): Disabled`
19? Enable conflict detection?
20 `Yes`
21? Select the default resolution strategy
22 `Auto Merge`
23? Here is the GraphQL API that we will create. Select a setting to edit or continue:
24 `Continue`
25? Choose a schema template
26 `Single object with fields (e.g., “Todo” with ID, name, description)`

Troubleshooting: Cloud sync will fail without the conflict detection configuration. To enable it for an existing project, run amplify update api and choose Enable conflict detection (required for DataStore).

The amplify add api command adds a new AmplifyConfig group to your Xcode project. It will contain the following files:

  • AmplifyConfig/
    • amplifyconfiguration.json
    • awsconfiguration.json
    • schema.graphql

Idiomatic persistence

DataStore relies on platform standard data structures to represent the data schema in an idiomatic way. The persistence language is composed by data types that satisfies the Model interface and operations defined by common verbs such as save, query and delete.

Data schema

The first step to create an app backed by a persistent datastore is to define a schema. DataStore uses GraphQL schema files as the definition of the application data model. The schema contains data types and relationships that represent the app's functionality.

Sample schema

For the next steps, let's start with a schema for a small blog application. Currently, it has only a single model. New types and constructs will be added to this base schema as more concepts are presented.

Open the schema.graphql file located by default at amplify/backend/{api_name}/ and define a model Post as follows.

1type Post @model {
2 id: ID!
3 title: String!
4 status: PostStatus!
5 rating: Int
6 content: String
7}
8
9enum PostStatus {
10 ACTIVE
11 INACTIVE
12}

Now you will to convert the platform-agnostic schema.graphql into platform-specific data structures. DataStore relies on code generation to guarantee schemas are correctly converted to platform code.

Like the initial setup, models can be generated either using the IDE integration or Amplify CLI directly.

Code generation: Platform integration

On iOS the amplify CLI offers an Xcode integration that automatically adds the Amplify-specific files to your project.

  1. Run the command:

    1amplify codegen models
  2. The amplify codegen models command adds a new AmplifyModels group to your Xcode project. It will contain the following files:

  • AmplifyModels/
    • AmplifyModels.swift
    • Post.swift
    • Post+Schema.swift
    • PostStatus.swift
  1. Build the project (Cmd+b).

Code generation: Amplify CLI

Models can also be generated using the Amplify CLI directly.

In your terminal, make sure you are in your project/root folder and execute the codegen command:

1amplify codegen models

You can find the generated files at amplify/generated/models/.

Initialize Amplify DataStore

To initialize the Amplify DataStore call Amplify.add(plugin:) method and add the AWSDataStorePlugin, then call Amplify.configure().

Add the following imports to the top of your AppDelegate.swift file:

1import Amplify
2import AWSDataStorePlugin

Add the following code

In your App scene, configure Amplify in the initializer:

1@main
2struct MyAmplifyApp: App {
3
4 var body: some Scene {
5 WindowGroup {
6 ContentView()
7 }
8 }
9
10 init() {
11 do {
12 // AmplifyModels is generated in the previous step
13 let dataStorePlugin = AWSDataStorePlugin(modelRegistration: AmplifyModels())
14 try Amplify.add(plugin: dataStorePlugin)
15 try Amplify.configure()
16 print("Amplify configured with DataStore plugin")
17 } catch {
18 print("Failed to initialize Amplify with \(error)")
19 }
20 }
21}

Add to your AppDelegate's application:didFinishLaunchingWithOptions method

1func application(
2 _ application: UIApplication,
3 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
4) -> Bool {
5
6 do {
7 // AmplifyModels is generated in the previous step
8 let dataStorePlugin = AWSDataStorePlugin(modelRegistration: AmplifyModels())
9 try Amplify.add(plugin: dataStorePlugin)
10 try Amplify.configure()
11 print("Amplify configured with DataStore plugin")
12 } catch {
13 print("Failed to initialize Amplify with \(error)")
14 return false
15 }
16
17 return true
18}

Upon building and running this application you should see the following in your console window:

1Amplify configured with DataStore plugin

Persistence operations

Now the application is ready to execute persistence operations. The data will be persisted to a local database, enabling offline-first use cases by default.

Even though a GraphQL API is already added to your project, the cloud synchronization will only be enabled when the API plugin is initialized and the backend provisioned. See the Next steps for more info.

Writing to the database

To write to the database, create an instance of the Post model and save it.

1let post = Post(title: "Create an Amplify DataStore app",
2 status: .active)
3do {
4 try await Amplify.DataStore.save(post)
5 print("Post saved successfully!")
6} catch let error as DataStoreError {
7 print("Error saving post \(error)")
8} catch {
9 print("Unexpected error \(error)")
10}
1let post = Post(title: "Create an Amplify DataStore app",
2 status: .active)
3let sink = Amplify.Publisher.create {
4 try await Amplify.DataStore.save(post)
5}.sink {
6 if case let .failure(error) = $0 {
7 print("Error saving post \(error)")
8 }
9}
10receiveValue: {
11 print("Post saved successfully! \($0)")
12}

Reading from the database

To read from the database, the simplest approach is to query for all records of a given model type.

1do {
2 let posts = try await Amplify.DataStore.query(Post.self)
3 print("Posts retrieved successfully: \(posts)")
4} catch let error as DataStoreError {
5 print("Error retrieving posts \(error)")
6} catch {
7 print("Unexpected error \(error)")
8}
1let sink = Amplify.Publisher.create {
2 try await Amplify.DataStore.query(Post.self)
3}.sink {
4 if case let .failure(error) = $0 {
5 print("Error retrieving posts \(error)")
6 }
7}
8receiveValue: { posts in
9 print("Posts retrieved successfully: \(posts)")
10}

Next steps

Congratulations! You’ve created and retrieved data from the local database. Check out the following links to see other Amplify DataStore use cases and advanced concepts: