Zubair Akber
Zubair Akber

Reputation: 2828

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") },
    {}
)

Upvotes: 0

Views: 433

Answers (2)

Zubair Akber
Zubair Akber

Reputation: 2828

So after struggling i just find out that the syncExpression is reevaluated whenever we call the Amplify.DataStore.start() method, so what we need to do in this case is that we have to set the configurations once with the required syncExpressions, so whenever we call the DataStore.start it just reevaluate the expression and before calling the start method we just need to update the variables that we have used in the syncExpression so in our case it is AwsConstants.AMPLIFY_SUB_ID

Upvotes: 0

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

Upvotes: 1

Related Questions