Second question, if I have classes with one-to-man...
# store
b
Second question, if I have classes with one-to-many relationships, what is the best way to keep all of that in sync? Here’s an example
Copy code
data class Parent(
  val parentId: String,
  val children: List<Child>
)

data class Child(val childId: String)
Then we have entities that would look like
Copy code
data class ParentEntity(val parentId: String)
data class ChildEntity(val childId: String, parentId: String)
Is the best way to do keep it in sync is to have two source of truths?
Copy code
SourceOfTruth<ParentKey, Parent, ProfileEntity>
SourceOfTruth<ChildKey, Child, ChildEntity>
Then whenever a
Parent
is created/updated we update the
Child
through the child SoT
Copy code
class ParentSourceOfTruth(
  val dao: ParentDao,
  val childSourceOfTruth: ChildSourceOfTruth
) : SourceOfTruth<ParentKey, Parent, ParentEntity> {
  ...
  // create example
  override fun write(key: ParentKey, value: Parent) {
    dao.insert(value.toEntity())
    
    value.children.forEach { child ->
      childSourceOfTruth.write(ChildKey.Write(value.parentId), child)
    }
  }
}
This lets us create/write the
Parent
as a whole, or if the
Child
is modified, we can change just the with the child SoT.
m
Hey Blake, Sorry to be slow. Working with lists is a problem that has come up a lot. We introduced list decomposition here https://github.com/MobileNativeFoundation/Store/pull/548 but it wouldn't be a clean solution for this specific problem. I've been thinking about higher-level data management and normalization a lot recently. I'm curious what you ended up with?
b
I ended up with the solution with the most code. Each relation gets its own store and the parent store also updates everything including the relations through the same dao’s. I’ll probably abstract it out but I just wanted to get something working.