androiddatastoreaws-amplifyaws-amplify-sdk-android

Download selective data from DataStore based on sub id Amplify


I just want to download selective data based on user sub id but Amplify is allowing me to run the configure command once, selective syncing is used for this purpose, I am using amplify signin method and getting the sub id from it, I just want to fetch the data based on that sub id but I have to re-configure the Amplify so that will be based on that sub id but for Amplify Auth service Amplify is already configured and I am not able to add configuration based on sub id

Here is the code that I am using in my application class

/* Add the Amplify Plugins */
Amplify.addPlugin(AWSApiPlugin())
Amplify.addPlugin(AWSCognitoAuthPlugin())

Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration(
    DataStoreConfiguration.builder()
        .syncExpression(Rooms::class.java) { Devices.SUB.ge(AwsConstants.AMPLIFY_SUB_ID) }
        .syncExpression(Scenes::class.java) { Devices.SUB.ge(AwsConstants.AMPLIFY_SUB_ID) }
        .syncExpression(Devices::class.java) { Devices.SUB.ge(AwsConstants.AMPLIFY_SUB_ID) }
        .syncExpression(Automations::class.java) { Devices.SUB.ge(AwsConstants.AMPLIFY_SUB_ID) }
        .syncExpression(MasterNodes::class.java) { Devices.SUB.ge(AwsConstants.AMPLIFY_SUB_ID) }
        .build()
).build())

Amplify.configure(applicationContext)

I just want to add the configuration to downlaod the selective data before getting the data ready

/* Starting the DataStore Syncing */
Amplify.DataStore.start(
    { Log.i(Constants.TAG_AMPLIFY, "DataStore started") },
    {}
)

Solution

  • I faced the same issue. Here is the key concept to solve this.

    Although the Amplify.configure() is called once, but each time you do datastore start, your syncExpression function gets reevaluated.

    So, set the sub id globally (lets say shred pref) and do a datastore start.

    For example,

     Amplify.addPlugin(AWSDataStorePlugin.builder().dataStoreConfiguration(
                        DataStoreConfiguration.builder()
                                .syncExpression(Chef.class, () -> Chef.EMAIL.eq(getmailid("Chef")))
                                .syncExpression(Order.class, () -> Order.EMAIL.eq(getmailid("Order")))
                                .syncExpression(OrderDishes.class, () -> OrderDishes.EMAIL.eq(getmailid("OrderDishes")))
                                .syncExpression(Dish.class, () -> Dish.EMAIL.eq(getmailid("Dish")).or(isCustomer()))
                               // .syncExpression(Dish.class, () -> (isCustomer()))
                                .build())
                        .build());
    

    Here the mail id is set later and then when datastore start happens getmailid gets called again