1 year ago

#184315

test-img

Austin E

Best Way to Manage Derived Properties

I have a couple custom NSManagedObjects that have various relationships between each other. Below is a very simplified example. In production there should ~10 instances of A, >= 10k instances of B, and <30 instances of C.

First, I'm trying to track the sum of B.value for specific categories in A. Second, I'm tracking the sum of B.value in C if B.date is between C.startDate and C.endDate. C instances are in a linked list style representing sequential windows in time.

If B.value changes manually going to A and C is fairly simple and updating the cached value in each. Updating the date in B is a little tougher as I'd have to search through the list and update it.

With all of this in mind, I've been trying to determine what is the best way in core data to keep these cached values up to date? My current thought is a mediator pattern, NotificationCenter, or KVO. Mediator pattern is not super flexible but would work. NotificationCenter seems ideal, however I'm not sure how to ensure that all instances of C are always in memory and subscribed to the publisher. KVO seems solid, but it doesn't seem to report edits for objects already in to-many relationships. What is the best way to keep these objects in sync with each other in the most Core data-esc way?

+---------+            +--------+            +---------+
|A        |            |B       |            |C        |
+---------+            +--------+            +---------+
|totalCatA| <------->> |category| <<-------> |total    |
|totalCatB|            |date    |            |startDate|
+---------+            |value   |            |endDate  |
                       +--------+            |prevC    |
                                             |nextC    |
                                             +---------+

swift

core-data

nsnotificationcenter

key-value-observing

mediator

0 Answers

Your Answer

Accepted video resources