简体   繁体   English

如何使UI反映HealthKit

[英]How to make UI reflect HealthKit

I am experimenting with HealthKit for a future project, and am currently implementing a single view calorie counter. 我正在为将来的项目试验HealthKit,目前正在实现单视图卡路里计数器。 A simple text field to input a calorie amount and progress indicator which shows calories/daily allotment. 一个简单的文本字段,用于输入卡路里量和进度指示器,该指示器显示卡路里/每日分配量。 There is no data model (other than calorieGoal in NSUserDefaults) as it is built entirely on top of HealthKit. 没有数据模型(NSUserDefaults中的卡路里目标除外),因为它完全建立在HealthKit之上。

The progress indicator should be persistent between launches and reset daily, but I am having difficulty doing this in an efficient manner. 进度指示器应该在发射和每天重置之间保持不变,但是我很难以有效的方式做到这一点。 The two solutions I have come up with are: 我提出的两个解决方案是:

  1. fetch Dietary Calories from HealthKit every time the view is loaded. 每次加载视图时,都从HealthKit中获取卡路里。 This seems to be costly for a simple task, but I am leaning towards it. 对于一个简单的任务来说,这似乎是昂贵的,但是我倾向于它。
  2. create another NSUserDefualts entry to keep track of the number of calories and the date the last entry was made, then check the date every time the view is loaded/app is launched. 创建另一个NSUserDefualts条目以跟踪卡路里的数量和最后一次输入的日期,然后在每次加载视图/启动应用程序时检查日期。

I feel like there should be a better solution, but I am unable to come up with one. 我觉得应该有一个更好的解决方案,但是我无法提出一个解决方案。

Have a data model in memory. 在内存中有一个数据模型。 Your model contains the variables that hold your data. 您的模型包含保存数据的变量。

Your model also is responsible for the code that deals with the data. 您的模型还负责处理数据的代码。 This includes the code that interacts with HealthKit. 这包括与HealthKit交互的代码。

Your model knows nothing about your UI. 您的模型对用户界面一无所知。 It does not import any UI related modules. 它不会导入任何与UI相关的模块。 It could probably run on iPhone and watches. 它可能可以在iPhone和手表上运行。

Since you get your data asynchronously, your model sends a notification, when it received new data. 由于您异步获取数据,因此模型在收到新数据时会发送一条通知。

Your UI code listens for notifications when there is new data. 当有新数据时,您的UI代码将侦听通知。 When your UI gets such a notification, it redisplays the model data in the main queue. 当您的UI收到此类通知时,它将在主队列中重新显示模型数据。

This approach works for your simple case up to very complex apps. 这种方法适用于您的简单案例,甚至适用于非常复杂的应用程序。

Usage example for the startup case: 启动案例的用法示例:

  • start your app 启动你的应用
  • tell your model it should asynchronously fetch the data from hk. 告诉您的模型,它应该异步从hk提取数据。
  • your model also handles authorisation (makes your UI code simpler). 您的模型还处理授权(使您的UI代码更简单)。
  • when the model finally asynchonously received the data from HealthKit, it sends the notification. 当模型最终异步接收到来自HealthKit的数据时,它将发送通知。
  • when your UI receives the notification, it redisplays the data from your model. 当用户界面收到通知时,它将重新显示模型中的数据。

This aporoach also handles the case, when hk data is changed while your app runs (changed by another app or measured by the watch). 当您的应用程序运行时hk数据发生更改(由另一个应用程序更改或由手表衡量)时,此漏洞也可以处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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