I am having some trouble fixing a race condition w...
# coroutines
j
I am having some trouble fixing a race condition where there are multiple executions on the same function , I was thinking of adding a field marking the status of the object that is still in progress, but is there any other elegant way to address this?
b
How will you be preventing this field from experiencing race conditions with shared mutable state?
l
sounds like you want to do @synchronized
u
Are you developing the UI or the Backend?
j
@uli Backend sir
u
It really depends. With coroutines, you could use an actor pattern instead of
withLock
. As you are talking about a database, you might also want to look into database transactions / locking.
l
What I would do is when the user clicks a button launch a job in the backend, if he clicks it again and the job is active you can cancel the first job and launch a new one. Or it will just return the same one that was first launched.
Copy code
job = launch { your code }