Rafael Costa
01/16/2024, 5:02 PMforceSync
) but the "waiting on the result" part should be.
This is what I have now:
suspend fun forceSync(): Result<Unit, Error> {
return deferredForceSyncNow().await()
}
private fun deferredForceSyncNow() = async { // this class implements CoroutineScope
syncNow(isForceSync = true) //syncNow is another suspend function that returns a Result<Config, Error>
.map { /* Unit */ }
}
So the idea here is I made the deferredForceSyncNow
return a Deferred object right away and then if the forceSync
is called on a job that gets cancelled, the await
is what gets cancelled, not the actual "syncing".
I am wondering though, is this the best we can do? And does it work as I expect it to? I found it a little weird, but it looks alright so far 🤔Sam
01/16/2024, 5:18 PMforceSync()
function to be cancelable without actually canceling the sync job, you're basically saying that the function that calls forceSync()
is not the owner of the sync job. That means the sync job needs a separate owner, which is the coroutine scope belonging to the class.Sam
01/16/2024, 5:18 PMforceSync()
function joins the sync task in a cancelable way. An await()
is just a join()
that returns a result, so it fits well here.Rafael Costa
01/16/2024, 5:23 PM