MobileNativeFoundation / MobileNativeFoundation/Store
Two stores backed by the same source of truth doesn't work
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 3.4k
- Forks
- 217
- Avg merge
- 18m
- Merged PRs (30d)
- 4
Description
As @digitalbuddha recommends (https://github.com/dropbox/Store/issues/193#issuecomment-671104053), I'm backing my two stores with the same source of truth:
Given the following two stores:
UsersStore: Store<Unit, List<User>>UserStore: Store<Int, User>
and both using the same db table as Source of Truth,
and the server containing the following 4 users: A, B, C and D:
userStore.get("A") => returns User A, and also adds to the user table: User A- Right now the users table only has the User A saved.
- When I call
usersStore.get(Unit), I'm now only getting User A (while if the fetcher would have been called, User A, B, C and D would have been returned).
Probably cause: when I'm asking the usersStore to get all the users, it first tries to read the Source of truth to see if anything is in there (which there is: User A, but User A is obviously not all users).
This is a really simple Store usecase, so I guess I must be doing something wrong or missing something.
Code:
// UsersStore.kt
sourceOfTruth = SourceOfTruth.of(
reader = {
db.userRowDao().all()
.map { userRows ->
userRows.map { userRow ->
userRow.toModel()
}
}
},
writer = { _, users->
val rows = users.map { it.toRow() }
db.userRowDao().insert(*rows.toTypedArray())
},
delete = {
db.userRowDao().deleteAll()
},
deleteAll = {
db.userRowDao().deleteAll()
}
)
// UserStore.kt
sourceOfTruth = SourceOfTruth.of(
reader = { userId->
db.userRowDao().byId(userId)
.map { userRow ->
userRow ?.toModel()
}
},
writer = { _, user ->
db.userRowDao().insert(user .toRow())
},
delete = { userId ->
db.userRowDao().deleteById(userId )
},
deleteAll = {
db.userRowDao().deleteAll()
}
)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the UsersStore and UserStore SourceOfTruth configurations in the issue, especially the db.userRowDao() readers and writers. Reproduce the sequence where User A is stored through UserStore and usersStore.get(Unit) is called afterward. Done means the shared table behavior is understood and the reported all-users result is corrected or documented with a verified explanation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100