Created
December 12, 2025 09:28
-
-
Save ghasemdev/62aa0fdf95632f390ffe9e6fd287722f to your computer and use it in GitHub Desktop.
Exercise 8: Local-First Loading with DB + Network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()}" } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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