Skip to content

contentstack/contentstack-ios-sync-playground

Repository files navigation

Build a playground app using Sync API with Contentstack’s iOS SDK

This is a demo playground app built using Contentstack’s iOS SDK and Sync API. You can try out and play with our Sync API with this example app, before building bigger and better applications.

Perform the steps given below to use this example app.

Prerequisites

In this tutorial, we will first go through the steps involved in configuring Contentstack and then look at the steps required to customize and use the presentation layer.

Step 1: Create a stack

Log in to your Contentstack account, and create a new stack. Read more about stacks.

Step 2: Add a publishing environment

Add a publishing environment to publish your content in Contentstack. Provide the necessary details as per your requirement. Read more about environments.

Step 3: Import content types

For this app, we need one content type: Session. Here’s what it is needed for:

  • Session: Lets you add the session content to your app

For quick integration, we have already created the content type. Download the content types and import it to your stack. (If needed, you can create your own content types. Read more about Content Types.)

Now that all the content types are ready, let’s add some content for your Sync Playground app.

Step 4: Adding content

Create and publish entries for the ‘Session’ content type.

Now that we have created the sample data, it’s time to use and configure the presentation layer.

Step 5: Clone and configure the application

To get your app up and running quickly, we have created a sample playground app. Clone the Github repo given below and change the configuration as per your need:

$ git clone https://github.com/contentstack/contentstack-ios-sync-playground.git

Now add your Contentstack API Key, Delivery Token, and Environment to the APIManager.swift file within your project. (Find your Stack's API Key and Delivery Token).

class StackConfig {

    static var APIKey = "***REMOVED***"
    static var AccessToken = "DELIVERY_TOKEN"
    static var EnvironmentName = "ENVIRONMENT"
    static var _config : Config {
        get {
            let config = Config()
            config.host = "URL"
            return config
        }
    }
}

This will initiate your project.

Step 6: Initialize sync

To perform initial sync, use the sync method, which fetches all the content of the specified environment.

APIManager.stack.sync {[weak self] (stack, error) in
    guard let slf = self, let syncStack = stack else {return}
    if (error == nill) {
        syncStack.items.count;
    }
}

On successful sync completion, you will get a sync token in response, which you need to use to get subsequent (delta) syncs.

Step 7: Use pagination token

If the result of the initial sync contains more than 100 records, the response would be paginated. In that case, it returns a pagination token. While the SDK continues to automatically fetch the next batch of data using the pagination token, it comes in handy in case the sync process is interrupted midway (due to network issues, etc.). You can use it to reinitiate sync from where it was interrupted.

APIManger.stack.syncPaginationToken(<pagination_token>, completion: {[weak self] (stack, error) in
    guard let slf = self, let syncStack = stack else {return}
    if (error == nill) {
        syncStack.items.count;
    }
})

Step 8: Publish new entries

In order to understand how you can also fetch only new (incremental) updates that were done since the last sync, you should create more entries and publish them. You can then use the Subsequent Sync call given below to see how it works.

Step 9: Perform subsequent sync

In the response of the initial sync, you get a sync token in the response. This token is used to fetch incremental updates, i.e., only the changes made after the initial sync. Use the syncToken method to perform subsequent syncs.

APIManager.stack.syncToken(<sync_token>, completion: { (syncStack:SyncStack, error:NSError) in
    guard let slf = self, let syncStack = stack else {return}
    if (error == nill) {
        syncStack.items.count;
    }
]
})

More Resources

About

iOS playground app that lets you try out Contentstack’s Sync API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •