Kodeco Forums

CareKit Tutorial for iOS: Part 2

In this 2-part CareKit Tutorial for iOS, learn how easy it is to build an iOS app that helps users manage and understand their personal health.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/928-carekit-tutorial-for-ios-part-2

Hi Jeff - great tutorial! Thanks so much for the wonderfully detailed walkthrough. I do have a question…

When creating the Assessment Bar Chart in InsightsDataManager.swift using fetchActivityResultsFor(_ activity: startDate: endDate: completionClosure:), the result is stored in fetchedData with the event date as the key. You mentioned that this implementation is not for more than one assessment per type each day.

How would you change this implementation if there were more than one assessment for each type per day?

Thanks and Regards,
Mike

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!

Hi jeff, good work. Its really a great help for me. But i want one more help from your side is how can we save multiple values of each assessment on each day based on timestamps in careplanstore db?

Hi Donald,

As mentioned in the prior answer, the model you use to pass around data retrieved in my implementation is:

[DateComponents: Double]

This would actually work fine, if the value being used for DateComponents was unique out to a temp stamp. However, the only logical source for that timestamp is the NSDateComponents date property of OCKCarePlanEvent. The problem with that is that CareKit only stores the year, month and day.

One possible approach would be to store an array of Double’s as the value of this model, as I suggested in the prior response. In fact, OCKCarePlanEvent has an occurrenceIndexOfDay property which contains the index of a particular event on a given date. So when you fetch these, you could sort them by that index, and build the array with that (though the order may or may not matter to you).

In short, I’d do exactly what I recommended in the prior answer and use an array for the value.

Hope that helps!

Hi Jeff,
Great article big help.
I have one question regarding the placeholder displayed on task controller in research kit how can we customise the placeholder
for example - in heart rate it’s showing count/min need to customise it to beats/min

Thanks and Regards
Rahul

Thank you for this tutorial! I’ve been searching for help with integrating ResearchKit surveys and active tasks into a CareKit application. Can you help?

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]