0

I'm using AWS Amplify v2.3.0 on Android Studio. Looking AWS documentation I can use Kotlin-Callback or Kotlin-Coroutines code. The callbacks call works for me. But the coroutines not working because IDE tell me that needs more parameters.

I need to use coroutines and not callbacks.For example, the sample AWS Code with callbacks works form me:

private fun getTodo(id: String) {
    Amplify.API.query(ModelQuery.get(Todo::class.java, id),
        { Log.i("MyAmplifyApp", "Query results = ${(it.data as Todo).name}") },
        { Log.e("MyAmplifyApp", "Query failed", it) }
    );
}

But the coroutine AWS sample code not works, because query function needs more parameters.

suspend fun getTodo(id: String) {
   try {
       val response = Amplify.API.query(ModelQuery.get(Todo::class.java, id))
       Log.i("MyAmplifyApp", response.data.name)
   } catch (error: ApiException) {
       Log.e("MyAmplifyApp", "Query failed", error)
   }
}

Anyone know what is happening?

Thank you so much!

1 Answers1

1

Use the AWS SDK for Kotlin to write Android Apps. The AWS SDK for Kotlin supports Coroutines. See this doc topic in the Kotlin DEV Guide:

https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/coroutines.html

Using this SDK, you can invoke AWS services from an Android project. For details, see this Android example in the AWS Code Library that shows you how to create an Android app that invokes AWS Services in an Android Studio project:

https://docs.aws.amazon.com/code-library/latest/ug/cross_SnsPublishSubscription_kotlin_1_topic.html

See:

enter image description here

smac2020
  • 9,637
  • 4
  • 24
  • 38
  • Than you so much for your answer. But I need to use Amplify Kotlin SDK to use functionalities like this https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/android/. And seems that Amplify SDK supports coroutines with when use the sample code not works... – Dani Castaño Mar 20 '23 at 06:00
  • If I had your requirments and needed to use GraphQL from Android, i would look at using Kotlin SDK and AWS Appsync. – smac2020 Mar 20 '23 at 12:30