Mauker1/MaterialSearchView

Fix race condition in V 2.0 to avoid DB inconsistencies

Closed this issue · 1 comments

The Problem

Since MSV 2.0 has migrated to use Room + Coroutines a concurrency problem showed up in the alpha versions. To reproduce it try the following steps below.

Say that we call the following methods in order:

val suggestions = repository.fetchSuggestions() // Getting suggestions from somewhere else
searchView.addSuggestions(suggestions)
searchView.addPin(suggestions.first())

The addSuggestions() and addPin() methods will launch each a suspend function block of code to execute their respective blocks of code. Here's where the problem lies. There's no guarantee that the suggestions will be added before the pin is added. Sometimes that may be true, but sometimes it isn't and since the query strings are unique in the database if you try to add the exact same String it'll replace the one that was added before. Effectively, in this case, it means that the pin might get overridden by the suggestion with the same String.

This problem has the potential to cause other inconsistencies in the database.

Possible solution

If we enqueue the suspend fun blocks of code and somehow guarantee strict sequential execution, each block will execute in the same order it was called by the MSV API.

Fixed using Actors.