epam / CoroutinesCache

In mobile development exists solution for caching with RxJava usage, but there is no solutions for Kotlin Coroutines. The project is to provide this functionality to mobile community.
Apache License 2.0
162 stars 7 forks source link

Inflight requests #17

Open PaulWoitaschek opened 5 years ago

PaulWoitaschek commented 5 years ago

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.