3

I'm struggling to find a way to use dagger to inject a DynamoDB mapper into my Repository.

I'm using AwsMobileClient which requires initialising in my "main activity" like so:

    AWSMobileClient.getInstance().initialize(application) {
        Timber.d("AWSMobileClient is initialized")
    }.execute()

Currently I'm creating a mapper inline in my repository like so:

    val credentials = AWSMobileClient.getInstance().credentialsProvider
    val config = AWSMobileClient.getInstance().configuration
    val ddbClient = AmazonDynamoDBClient(credentials)
    ddbClient.setRegion(Region.getRegion(Regions.EU_WEST_2))
    val mapper = DynamoDBMapper.builder()
            .awsConfiguration(config)
            .dynamoDBClient(ddbClient)
            .build()

If i move that out to a dagger module it looks like this:

@Singleton
@Provides
AmazonDynamoDBClient provideDynamoDbClient() {
    return new AmazonDynamoDBClient(AWSMobileClient.getInstance().getCredentialsProvider());
}

@Singleton
@Provides
DynamoDBMapper provideDynamoDBMapper(AmazonDynamoDBClient ddbClient) {
    return DynamoDBMapper.builder()
            .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
            .dynamoDBClient(ddbClient)
            .build();
}

However this crashes and burns because dagger tries to create the objects before the AwsMobileClient has finished initialising and the credentials provider is null when trying to create the DynamoDBClient

Not sure if i'm going about this the wrong way and all the samples don't use dependency injection so can't steal ideas from there.

OliverDeLange
  • 643
  • 5
  • 19

0 Answers0