skip to Main Content

I am trying to query data from DynamoDB in my android app using kotlin. The code should be correct, but looking at the logs, it doesn’t seem to be executing the code since I’m not receiving any type of results from it. Wondering if anyone has any idea what is happening based off the logs?

(https://phpout.com/wp-content/uploads/2023/04/LzZ5D.png)

fun getUserData(email: String) {
    println("We will get the userdata!!!!!!!!")
    Amplify.DataStore.query(
        User::class.java,
        { items ->
            while (items.hasNext()) {
                val item = items.next()
                Log.i("Amplify", "Queried item: " + item.name)
            }
        },
        { failure -> Log.e("Tutorial", "Could not query DataStore", failure) }
    )
}

023-04-21 19:41:19.981 16806-16893 TrafficStats com.example.aupairconnect D tagSocket(119) with statsTag=0xffffffff, statsUid=-1
2023-04-21 19:41:20.011 16806-16806 Compatibil…geReporter com.example.aupairconnect D Compat change id reported: 171228096; UID 10162; state: ENABLED
2023-04-21 19:41:20.211 16806-16881 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock released.
2023-04-21 19:41:20.211 16806-16881 MyAmplifyApp com.example.aupairconnect I DataStore started
2023-04-21 19:41:20.531 16806-16887 e.aupairconnect com.example.aupairconnect W Long monitor contention with owner pool-5-thread-1 (16886) at void com.amplifyframework.api.aws.SubscriptionEndpoint.requestSubscription(com.amplifyframework.api.graphql.GraphQLRequest, com.amplifyframework.api.aws.AuthorizationType, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Action)(SubscriptionEndpoint.java:196) waiters=0 in void com.amplifyframework.api.aws.SubscriptionEndpoint.requestSubscription(com.amplifyframework.api.graphql.GraphQLRequest, com.amplifyframework.api.aws.AuthorizationType, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Action) for 562ms
2023-04-21 19:41:20.666 16806-16888 e.aupairconnect com.example.aupairconnect W Long monitor contention with owner pool-5-thread-1 (16886) at void com.amplifyframework.api.aws.SubscriptionEndpoint.requestSubscription(com.amplifyframework.api.graphql.GraphQLRequest, com.amplifyframework.api.aws.AuthorizationType, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Action)(SubscriptionEndpoint.java:196) waiters=1 in void com.amplifyframework.api.aws.SubscriptionEndpoint.requestSubscription(com.amplifyframework.api.graphql.GraphQLRequest, com.amplifyframework.api.aws.AuthorizationType, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Consumer, com.amplifyframework.core.Action) for 695ms
2023-04-21 19:41:20.793 16806-16880 amplify:aws-datastore com.example.aupairconnect I Started subscription processor for models: [User] of types [ON_CREATE, ON_UPDATE, ON_DELETE].
2023-04-21 19:41:20.801 16806-16893 TrafficStats com.example.aupairconnect D tagSocket(120) with statsTag=0xffffffff, statsUid=-1
2023-04-21 19:41:20.975 16806-16860 EGL_emulation com.example.aupairconnect D app_time_stats: avg=73.29ms min=0.58ms max=656.79ms count=17
2023-04-21 19:41:21.041 16806-16806 OnBackInvokedCallback com.example.aupairconnect W OnBackInvokedCallback is not enabled for the application.
Set ‘android:enableOnBackInvokedCallback="true"’ in the application manifest.
2023-04-21 19:41:21.060 16806-16904 amplify:aws-datastore com.example.aupairconnect I Successfully sync’d down model state from cloud.
2023-04-21 19:41:21.061 16806-16880 amplify:aws-datastore com.example.aupairconnect I Starting processing subscription data buffer.
2023-04-21 19:41:21.061 16806-16880 amplify:aws-datastore com.example.aupairconnect I Started the orchestrator in API sync mode.
2023-04-21 19:41:21.061 16806-16879 amplify:aws-datastore com.example.aupairconnect I Started processing the mutation outbox. Pending mutations will be published to the cloud.
2023-04-21 19:41:21.068 16806-16806 System.out com.example.aupairconnect I We will get the userdata!!!!!!!!
2023-04-21 19:41:21.068 16806-16806 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock acquired.
2023-04-21 19:41:21.068 16806-16885 amplify:aws-datastore com.example.aupairconnect I DataStore plugin initialized.
2023-04-21 19:41:21.074 16806-16806 System.out com.example.aupairconnect I We will get the userdata!!!!!!!!
2023-04-21 19:41:21.321 16806-16885 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock released.
2023-04-21 19:41:21.321 16806-16806 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock acquired.
2023-04-21 19:41:21.322 16806-16882 amplify:aws-datastore com.example.aupairconnect I DataStore plugin initialized.
2023-04-21 19:41:21.575 16806-16882 amplify:aws-datastore com.example.aupairconnect I Orchestrator lock released.
2023-04-21 19:41:24.656 16806-16911 ProfileInstaller com.example.aupairconnect D Installing profile for com.example.aupairconnect

(https://phpout.com/wp-content/uploads/2023/04/auf0U.png)

I tried looking through the Amplify’s documentation for more clarification on if I’m missing anything, but it looks like it is all there.

2

Answers


  1. Chosen as BEST ANSWER

    I found that the data wasn't coming through due to the data not being in the AmplifyDataStore Table on DynamoDb, but strangely it was in the User Table. Deleting and then saving the data again in the app, it was able to save to both tables, therefore able to be accessed by the API.


  2. If you want to use Kotlin to invoke Amazon DynamoDB (and other AWS Services), then I strongly recommend that you use the AWS SDK for Kotlin and the DynamoDbClient. You can read the DEV Guide here:

    AWS SDK for Kotlin Developer Guide

    To query data from an Amazon DynamoDB table using the Kotlin SDK, use this code:

    package com.kotlin.dynamodb
    
    // snippet-start:[dynamodb.kotlin.query.import]
    import aws.sdk.kotlin.services.dynamodb.DynamoDbClient
    import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
    import aws.sdk.kotlin.services.dynamodb.model.QueryRequest
    import kotlin.system.exitProcess
    // snippet-end:[dynamodb.kotlin.query.import]
    
    /**
    Before running this Kotlin code example, set up your development environment,
    including your credentials.
    For more information, see the following documentation topic:
    https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html
    */
    
    suspend fun main(args: Array<String>) {
    
        val usage = """
        Usage:
            <tableName>
        Where:
            tableName - The Amazon DynamoDB table to scan (for example, Music3).
            partitionKeyName - The partition key name of the Amazon DynamoDB table (for example, Artist).
            partitionKeyVal - The value of the partition key that should match (for example, Famous Band).
        """
    
        if (args.size != 3) {
            println(usage)
            exitProcess(0)
        }
    
        val tableName = args[0]
        val partitionKeyName = args[1]
        val partitionKeyVal = args[2]
    
        // For more information about an alias, see:
        // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
        val partitionAlias = "#a"
        val count = queryDynTable(tableName, partitionKeyName, partitionKeyVal, partitionAlias)
        print("There is $count item in $tableName")
    }
    
    // snippet-start:[dynamodb.kotlin.query.main]
    suspend fun queryDynTable(
        tableNameVal: String,
        partitionKeyName: String,
        partitionKeyVal: String,
        partitionAlias: String
    ): Int {
    
        val attrNameAlias = mutableMapOf<String, String>()
        attrNameAlias[partitionAlias] = partitionKeyName
    
        // Set up mapping of the partition name with the value.
        val attrValues = mutableMapOf<String, AttributeValue>()
        attrValues[":$partitionKeyName"] = AttributeValue.S(partitionKeyVal)
    
        val request = QueryRequest {
            tableName = tableNameVal
            keyConditionExpression = "$partitionAlias = :$partitionKeyName"
            expressionAttributeNames = attrNameAlias
            this.expressionAttributeValues = attrValues
        }
    
        DynamoDbClient { region = "us-east-1" }.use { ddb ->
            val response = ddb.query(request)
            return response.count
        }
    }
    // snippet-end:[dynamodb.kotlin.query.main]
    

    The Kotlin SDK is great to use within an Android Studio project as well. Here is a tutorial that shows you how:

    Creating a publish/subscription Android application that translates messages using the AWS SDK for Kotlin

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search