Hello everyone, I have a runtime data in my app th...
# coroutines
a
Hello everyone, I have a runtime data in my app that is constantly growing (aka analytics events). I want to be able to attach a solution to constantly dumping this data into a file so my Appium testing framework can access this file and check the expectations. How could I implement this solution to take into account that sometimes the data is being updated so often so I need to cancel the previous “file dump action” and start the new one? I was thinking about something very direct:
Copy code
private var fileDumpJob: Job? = null

private fun onDataUpdated() {
  fileDumpJob?.cancel()
  fileDumpJob = null
  fileDumpJob = launch {
     dumpDataToFile()
  }
}
Could there be a problem with this approach? Is there a more elegant solution?
s
Just to check—when you say "constantly growing", do you just need to append new lines to the end of the file, or do you actually need to replace the whole file every time? If the latter, your solution looks good, though you could also consider a
StateFlow
.
a
Good point. Indeed, At this moment of time it’s about appending a new lines to the file. But ideally I would like to have a generic solution so I am always sure that whatever in memory will be in the file.
My solution seems to be working - I am not sure about the performance tho. At the same time I see that even when the Job is cancelled I still keep getting the logs from the coroutine. How could it be?
s
How is your
dumpDataToFile()
function implemented? Cancellation is cooperative, and it's likely that your file-writing operation isn't cancellable.