Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Function in my Dao file is:

@Query("SELECT * from appData WHERE type = :type ORDER BY id DESC")
    fun getData(type: String): Flow<List<AppData>>

The database contains a column of automatically generated int primary key 'id', a column of string named 'type', and another string column named 'content'.

A Part of code from my composable function is:

val currentRetrievedDate: Date = Date()
    val currentDate: String = SimpleDateFormat("dd-MM-yyyy").format(currentRetrievedDate)

    var lastDateObjectList by remember { mutableStateOf(emptyList<AppData>()) }

    LaunchedEffect(streakCounterViewModel) {
        coroutineScope {
            streakCounterViewModel.appDataRepository.getDataStream("lastDate")
                .collect { newDataList ->
                    lastDateObjectList = newDataList as List<AppData>
                }
        }
    }

    if (lastDateObjectList.size>1){/*TODO*/
        for (i in 1 until lastDateObjectList.size){
            LaunchedEffect (Unit){
                streakCounterViewModel.deleteData(id = lastDateObjectList[i]!!.id,
                    type = lastDateObjectList[i]!!.type,
                    content = lastDateObjectList[i]!!.content)
            }
        }
    }

    val lastDateObject: AppData?/* = lastDateObjectList[0] ?: null*/
    lastDateObject = if (lastDateObjectList.isNotEmpty())
        lastDateObjectList[0]
    else null

        var lastDate = lastDateObject?.content ?: "00-00-0000"

    var currentStreakObjectList by remember { mutableStateOf(emptyList<AppData>()) }

    LaunchedEffect(streakCounterViewModel) {
        coroutineScope {
            streakCounterViewModel.appDataRepository.getDataStream("currentStreak")
                .collect { newDataList ->
                    currentStreakObjectList = newDataList as List<AppData>
                }
        }
    }

    if (currentStreakObjectList.size>1){
        for (i in 1 until currentStreakObjectList.size){
            LaunchedEffect (Unit){
            streakCounterViewModel.deleteData(id = currentStreakObjectList[i]!!.id,
                type = currentStreakObjectList[i]!!.type,
                content = currentStreakObjectList[i]!!.content)
            }
        }
    }

    val currentStreakObject: AppData?/* = currentStreakObjectList[0] ?: null*/
    currentStreakObject = if (currentStreakObjectList.isNotEmpty())
        currentStreakObjectList[0]
    else null

        var currentStreak = currentStreakObject?.content ?: "0"

In this code, the last login time and last streak of user is already saved, we just have to fetch the data from the local database, make sure that there are not more than 1 occurrences of same data type in the database, and then use that data. But this, instead of using the database values, uses 00-00-0000 as the lastDate, and uses "0" as the currentStreak (which should only be used for the first not, and in some rare situations only), and is not able to retrieve data from the database properly.

The data saving and updating logic is working fine, but only the retrieval part is causing some issue.

What I have tried:

I did tried to tweak it a little, but don't understand where and what should I exactly change to make it work properly!
Posted

The app seems to ignore the database values and defaults to "00-00-0000" for dates and "0" for streak counts, suggesting an issue with the data collection logic or state handling in the composable function.A likely cause could be incorrect data flow or state updates within the LaunchedEffect blocks, where the logic may not properly handle the list of data objects fetched from the repository. Ensuring that the list updates and state management are handled correctly could resolve the issue.

Verifying the data retrieval and update processes inside the Kotlin coroutines used might also help. Checking the database queries and making sure they fetch the correct data based on the app's requirements is essential.
 
Share this answer
 
You can try:

- Collect Data Properly: Instead of directly assigning the Flow to your state variables, collect the data within the LaunchedEffect block and then update the state variables accordingly.

- Separate Database Operations: Move the database operations (such as deletion) outside the LaunchedEffect blocks into a separate coroutine scope.

LaunchedEffect(Unit) {
    coroutineScope {
        val newDataList = streakCounterViewModel.appDataRepository.getDataStream("lastDate").first()
        lastDateObjectList = newDataList
        // Perform deletion operation if needed
        if (lastDateObjectList.size > 1) {
            for (i in 1 until lastDateObjectList.size) {
                launch {
                    streakCounterViewModel.deleteData(
                        id = lastDateObjectList[i]?.id ?: 0,
                        type = lastDateObjectList[i]?.type ?: "",
                        content = lastDateObjectList[i]?.content ?: ""
                    )
                }
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900