0

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.

The complete project is also uploaded on github: at HealthEase repo of tauqirnizami

Comments
  • 0
    Have you tried asking ChatGPT?
  • 2
    What‘s the question? What‘s the language? And why do you write that on devRant?
  • 0
    Case-sensitive matching of DB column names vs field/prop names in the code?

    Maybe the DTO type has fields rather than props and they're being ignored?

    That's a weird date format.

    SELECT * will bite you in the arse sooner or later.

    Not sure what you're using but it looks too complicated to work properly.
  • 0
    @donkulator I am doing android development using kotlin, jetpack compose, and SQLite libraries. I have my college 3rd year project to submit today itself and this last minute error isn't resolving :(
Add Comment