Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

AWS: AppSync – Real-time data with subscriptions.

by fragi
July 25, 2019
in AWS
Share on FacebookShare on Twitter

In the previous article (http://trikalabs.com/aws-appsync-swift/) we set up AppSync into our app. In this article, we will see how we can add real-time data functionality into our app by using subscribers.

Adding subscriptions is very easy, especially when the amplify has generated the code for us.

All we have to do is create the following code in the ViewController:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    var discard: Cancellable?
    
 
    func startSubscriptionForEvent() throws {
        
        do {
            discard = try appSyncClient?.subscribe(subscription: OnCreateTodoSubscription(), resultHandler: { (result, transaction, error) in
                if let result = result {
                    print(result.data!.onCreateTodo!.name + " " + result.data!.onCreateTodo!.description!)
                } else if let error = error {
                    print(error.localizedDescription)
                }
            })
        } catch {
            print("Error starting subscription.")
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        try? startSubscriptionForEvent()
        setupAppSync()
    }

 And let’s put a breakpoint inside the body of the resultHandler closure.

Now let’s go to the AWS console, navigate to AppSync. Select queries and trigger an event:

To execute the query, you have to log in. In order to find the ClientID you have to go to Cognito -> Manage User Pools -> select the user pool -> general settings -> App clients.

If everything is set up correctly the code execution stops at the breakpoint. 

At this point we can update our UI, so to keep it in sync with our server data.

Subscribers are very powerful because they are working using web sockets. That means we don’t have to poll for updates or ask the user to pull to refresh to get new data. If we set up subscribers, as soon we have new data the subscribers will be called automatically.

 

Tags: AmplifyAppSyncreal-time
fragi

fragi

Related Posts

AWS: Build data-driven apps with real-time and offline capabilities by using AppSync and Swift.
AWS

AWS: Build data-driven apps with real-time and offline capabilities by using AppSync and Swift.

July 25, 2019

In this article, we will use the AppSync framework to fetch and create data for our iOS app. By using...

AWS: Use Amazon PinPoint to collect analytics
AWS

AWS: Use Amazon PinPoint to collect analytics

July 25, 2019

In this article, we will add analytics to an iOS app by using Amplify and PinPoint. Create a project Firstly...

AWS: Integrate User Pools with Clean Architecture
AWS

AWS: Integrate User Pools with Clean Architecture

July 25, 2019

In the previous article(http://trikalabs.com/aws-integrating-user-pools-for-ios-apps/) we integrated User Pools into an iOS project. Also in the article http://trikalabs.com/clean-architecture/,we learned how to use...

AWS: Integrate User Pools on iOS Apps
AWS

AWS: Integrate User Pools on iOS Apps

July 25, 2019

Authenticate and manage users is a common task/need for many apps. In this article, we will set up an Amazon Cognito...

Next Post
Add SwiftLint to your iOS app

Add SwiftLint to your iOS app

Continuous Integration with TravisCI

Continuous Integration with TravisCI

Use Hound for automated code reviews

Use Hound for automated code reviews

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.