Search code examples
cloudkit

How To Sum CloudKit Record Values


Let's say you have a CloudKit type called Steps, and each step record has a value and a date. If you have 500 records, is there anyway to have CloudKit pass back the sum of all the values (ex: 5,490)?

I haven't found a way besides doing a CKQueryOperation for all the steps and then manually adding them. My concern with this is that if I ended up with a lot of step records, say 5,000, that CKQueryOperation might start to limit my results.


Solution

  • No, this isn't possible with CloudKit. You'd have to use something that allows server functions, like Parse.

    But this is a problem that is easily solved with your local device cache. If you sync your Step records with a local database (Realm, CoreData, SQLite, etc.) you can simply query all your steps in your database and add them up. This is the approach I would take.