Hi Mike. Thanks for the kind words!
This is a great question, as multiple results per type / day will not be uncommon in most applications of CareKit.
The model you chose to pass around data retrieved from the data store is totally up to you. In this case I chose:
[DateComponents: Double]
The date I use as a key comes from the date
property of OCKCarePlanEvent
which is a DateComponents
object that happens to only contain month day and year, meaning timestamps aren’t there to help differentiate.
For ZombieKit, because I knew I’d only written a single one, it was just easier to use that day as the key. If there was more than one item for that activity in a day, the subsequent additions would just overwrite the previous ones.
So if you wanted to handle more than one event in a day, using this same basic architecture, you’d want to use a different model. A simple solution that should work within the sample code is to use this model:
[DateComponents: [Double]]
This means that each date points to an array of Double
values. Rather than adding each new value directly to the dictionary, you’d check to see if an item for its date already existed, and add to that array if so. If no value yet existed, you’d create the array with the value you just read.
Changes would then be required in the all of the methods that subsequently deal with that fetchedData. But it also depends on your implementation needs. For instance, how would you want to display multiple pulse readings in a day on the barchart? You might average them, grab the max, or maybe even break them out into multiple bars.
If you went for an average, one place you’d need to make a change is in barSeriesFor(date:title:tintColor)
. Right now it assumes a single value per date, so you’d need to modify completionData.map
to return the average of all values for each date.
Clearly that’s the tip of the iceberg, but changing the model returned in the completion closure of fetchActivityResultsFor(_ activity: startDate: endDate: completionClosure:)
is the first step, and should lead you to the other changes.
I hope that helps!