Skip to content

Instantly share code, notes, and snippets.

@ghasemdev
Created December 12, 2025 09:28
Show Gist options
  • Select an option

  • Save ghasemdev/62aa0fdf95632f390ffe9e6fd287722f to your computer and use it in GitHub Desktop.

Select an option

Save ghasemdev/62aa0fdf95632f390ffe9e6fd287722f to your computer and use it in GitHub Desktop.
Exercise 8: Local-First Loading with DB + Network
object FakeApi {
suspend fun fetchItems(): List<String> {
// TODO use context swiching
delay(1500)
if ((1..10).random() < 5) {
throw IOException("Network error: Failed to reach server")
}
return List(10) { i -> "Item #${(100..999).random()}" }
}
}
object FakeDatabase {
private var items = mutableListOf<String>()
suspend fun readItems(): List<String> {
// TODO use context swiching
delay(500)
if ((1..10).random() < 3) {
throw IOException("Local DB corrupted: failed to read data")
}
return items
}
suspend fun saveItems(newItems: List<String>) {
// TODO use context swiching
delay(300)
// TODO implement save logic
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment