I want to create an application that logs when I have gone jogging and uses Core Data to store the information. I want to store each workout with the date, distance and time I have run. I also want to be able to display a Summary which contains the total amount of times I have gone to run and the total distance run.
In my disign, I could display the summary directly from the Workout objects. How many times I have run is just the amount of workout objects and I could sum up the distance in each Workout to obtain the total distance run. However, I think that the second operation is too costly because I have to scann the whole database every time I want to display that data (This is the same problem as in iTunes you want to display the total amount hours of music you have in your device). I could store this information in a property every time the app lunches, but I guess that would cause a slow start up. Because of that, I thought I rather have 2 coredata objects Summary and Workout:
+---------------------+ +---------------------+
|Summary | |Workout |
+---------------------+ +---------------------+
|totalDistance | <--------------->> |date |
|totalAmountOfWorkouts| |distance |
+---------------------+ |time |
+---------------------+
Now here it comes the question. How should Summary be updated?
I could manually update the totalDistance and totalAmountOfWorkouts. I imagine implementing some sort of updateWorkout method which is triggered every time I create a new Workout. However, I know that Coredata has already observation capabilities and could tell me when a new Workout object has been inserted and I could update Summary: KVO. I have never used KVO and I wonder if this is the right case to use KVO? But how do you do that? Is actually KVO the best approach to solve this problem or should I rather implement a protocol in Workout and assign Summary as a delegate? I vagally remember to have heard that the KVO pattern is difficult to debug.
Summarizing, my questions are:
Q1: Should I get totalDistance directly scanning the DB?
Q2: Should I use KVO or the delegate pattern?
Q3: How is totalDistance updated?
Use the simplest API available. Let’s say you have an array of fetched Workouts. Then simply do
Only if this doesn’t meet your performance requirements, consider KVO or other mechanisms for caching the distance value. Real-time computation is always preferable to caching if the performance is acceptable. But I find it hard to believe that CoreData couldn’t handle the amount of Workouts you could realistically do in a lifetime.