简体   繁体   中英

How to use KVO to update properties

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

float distance = [[fetchedObjects valueForKeyPath: @"@sum.distance"] floatValue]

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.

If you have alot of Workout objects and you're displaying them in a tableview with batchSize, instead of iterating over them all you can create a NSFetchRequest that performs that calculation on the SQL level which should be significantly better performance and memory wise.

an example at the following blog post http://iphonedevelopment.blogspot.co.il/2010/11/nsexpression.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM