CoroutinesCache icon indicating copy to clipboard operation
CoroutinesCache copied to clipboard

Inflight requests

Open PaulWoitaschek opened this issue 6 years ago • 0 comments

Currently the coroutine cache isn't very smart about concurrent requests:

So if we use your readme example but modify it to perform multiple requests concurrently:

class MainActivity : AppCompatActivity() {
    private val persistence by lazy { Repository(cacheDir) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        GlobalScope.launch (Dispatchers.Main) {
          repeat(2) {
            launch { persistence.getData().await() }
          }
        }
    }
}

It performs the network request twice. What would actually be desired here is to have some sort of mutex so the calls will suspend so we don't request the network here twice while a request is already inflight.

PaulWoitaschek avatar May 07 '19 07:05 PaulWoitaschek