https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Joseph PH

12/17/2019, 11:09 AM
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

bdawg.io

12/17/2019, 7:52 PM
How will you be preventing this field from experiencing race conditions with shared mutable state?
l

Luis Munoz

12/17/2019, 9:24 PM
sounds like you want to do @synchronized
u

uli

12/18/2019, 5:51 AM
Are you developing the UI or the Backend?
j

Joseph PH

12/18/2019, 5:52 AM
@uli Backend sir
u

uli

12/18/2019, 10:46 AM
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

Luis Munoz

12/19/2019, 4:31 PM
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 }
2 Views