Bhavin Solanki
Bhavin Solanki

Reputation: 510

Amplify Datastore.start() not working after call Stop method syncExpression in Android

I am using syncExpression at intial stage of application level.

var initialSyncExpression : QueryPredicate? = null
var campID : String? = "CAM#0"
var currentActivity: Activity? = null
    private set
companion object {
    @SuppressLint("StaticFieldLeak")
    lateinit var instance: ImBybeApplication
        private set

    @SuppressLint("StaticFieldLeak")
    lateinit var context: Context
        private set
}


override fun onCreate() {
    super.onCreate()
    instance = this
    Preferences(this)
    registerActivityLifecycleCallbacks(this)
    FirebaseApp.initializeApp(this)
    context = applicationContext

    try {
        Amplify.addPlugin(AndroidLoggingPlugin(LogLevel.VERBOSE))
        Amplify.addPlugin(AWSCognitoAuthPlugin())
        Amplify.addPlugin(AWSPinpointPushNotificationsPlugin())
        Amplify.addPlugin(AWSApiPlugin())
        initialSyncExpression =
            Imbybe.GSI1.ne(Constants.ConstantTableName.CAMPAIGNPARTICIPATION)
                .and(Imbybe.GSI1.ne(Constants.ConstantTableName.redemptionLog))
                .and(Imbybe.GSI1.ne(Constants.ConstantTableName.raiseHandLog))

        Amplify.addPlugin(AWSDataStorePlugin.builder()
            .dataStoreConfiguration(
                DataStoreConfiguration.builder()
                    .syncExpression(Imbybe::class.java) {
                        Log.i("initialSyncExpression",
                            "onCreate: $campID :::: $initialSyncExpression"
                        )
                        initialSyncExpression
                    }
                    .build()
            )
            .build()
        )
        val configuration = AmplifyConfiguration.builder(applicationContext, R.raw.amplifyconfiguration)
            .devMenuEnabled(false)
            .build()

        CoroutineScope(Dispatchers.IO).launch {
            Amplify.Hub.subscribe(HubChannel.DATASTORE) { event: HubEvent<*> ->
                if (event.name == DataStoreChannelEventName.SYNC_QUERIES_READY.toString()) {
                    Preferences.instance?.isDataLoaded = true
                }
            }
        }
        Amplify.configure(configuration, applicationContext)

        DataStore.start({
            Log.i("JustCheckingError", "Started Datastore ")
        }, {
            Log.e("JustCheckingError", "getDataFromDatastore Error: $it")
        })
        Log.i("Tutorial", "Initialized Amplify")
    } catch (error: AmplifyException) {
        Log.e("Tutorial", "Could not initialize Amplify", error)
    }
}

And now i want campaignParticipation data after click on particular campaign. that code is below

    ImBybeApplication.instance.campID = drinkItems.campaignMaster?.pk ?: ""
    ImBybeApplication.instance.initialSyncExpression =
        Imbybe.GSI1.eq(Constants.ConstantTableName.CAMPAIGNPARTICIPATION)
                    .and(Imbybe.PK.eq(drinkItems.campaignMaster?.pk ?: ""))


    DataStore.stop(
        {
            Log.i("DataStore111", "DataStore stopped successfully.")
            DataStore.start(
                {
                    Log.i("DataStore111", "Startedddddd")
                },
                { error ->
                    Log.e("DataStore111", "Error starting DataStore", error)
                }
            )
        },
        { error ->
            Log.e("DataStore111", "Error stopping DataStore" + error.localizedMessage)
        }
    )

So by above code i got the all success logs, no error thrown by datastore or api. But in a datastore that campaignParticipation data is not getting.

Note - i have correct spelled and getting correct campaign ID at every-time.

Upvotes: 0

Views: 26

Answers (0)

Related Questions