blakelee
09/20/2024, 12:14 AMdata class Parent(
val parentId: String,
val children: List<Child>
)
data class Child(val childId: String)
Then we have entities that would look like
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?
SourceOfTruth<ParentKey, Parent, ProfileEntity>
SourceOfTruth<ChildKey, Child, ChildEntity>
Then whenever a Parent
is created/updated we update the Child
through the child SoT
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.Matthew Ramotar
10/18/2024, 5:01 PMblakelee
10/22/2024, 4:06 AM