Skip to content

//workflow/com.squareup.workflow1/Worker/doesSameWorkAs

doesSameWorkAs

[jvm]
Content
open fun doesSameWorkAs(otherWorker: Worker<*>): Boolean
More info

Override this method to define equivalence between Workers. The default implementation returns true if this worker’s class is the same as otherWorker‘s class.

At the end of every render pass, the set of Workers that were requested by the workflow are compared to the set from the last render pass using this method. Workers are compared by their declaredKType - including generics. Equivalent workers are allowed to keep running. New workers are started (run is called and the returned Flow is collected). Old workers are cancelled by cancelling their collecting coroutines. Workers for which doesSameWorkAs returns false will also be restarted.

Implementations of this method should not be based on object identity. Nor do they need to be based on anything including in the KType - such as generics - as those will already be compared by the Workflow Runtime, see WorkerWorkflow. For example, a Worker that performs a network request might check that two workers are requests to the same endpoint and have the same request data.

Most implementations of this method should compare constructor parameters.

E.g:

class SearchWorker(private val query: String): Worker {
// run omitted for example.

override fun doesSameWorkAs(otherWorker: Worker<*>): Boolean =
otherWorker is SearchWorker && otherWorker.query == query
}