简体   繁体   English

HealthKit,核心数据和CloudKit

[英]HealthKit, Core Data and CloudKit

I am building a fitness app with HealthKit integration. 我正在使用HealthKit集成构建健身应用程序。 Ultimately I would like to use CloudKit as well to a) allow data redundancy, but mainly b) to provide a few social features which require data to be in iCloud at least temporarily. 最终,我也想使用CloudKit来实现a)允许数据冗余,但是主要是b)提供一些社交功能,这些功能要求至少将数据临时存储在iCloud中。

I would like to be able to rely solely on HealthKit for data within the app, but feel that an alternate data model is necessary to persist data incase HealthKit permissions are revoked or not given in the first place. 我希望能够完全依靠HealthKit来获取应用程序中的数据,但是我认为,如果永久撤销或不授予HealthKit权限,则必须使用备用数据模型来保留数据。 I have chosen to stick with Core Data for this. 我选择坚持使用核心数据。

My question is how do I go about keeping my Core Data store and my HealthKit store in sync. 我的问题是如何使我的核心数据存储和HealthKit存储保持同步。 I have searched for an example on GitHub and for related questions on here, but cannot find any useful examples. 我已经在GitHub上搜索了一个示例,并在此处搜索了相关问题,但是找不到任何有用的示例。

Ultimately I will be then syncing the data in Core Data with CloudKit, but is the Core Data intermediary really necessary? 最终,我将使用CloudKit同步Core Data中的数据,但是Core Data中介真的必要吗?

With regards to App Store Review Guidelines 27.3 关于App Store审查指南27.3

Apps using the HealthKit framework that store users' health information in iCloud will be rejected 使用HealthKit框架在iCloud中存储用户健康信息的应用将被拒绝

I take this to mean that any Health data which was not created by your app cannot be stored in iCloud. 我的意思是,任何不是由您的应用创建的健康数据都无法存储在iCloud中。 There are many apps which store Health data on a third party server (ie RunKeeper). 有许多应用程序将运行状况数据存储在第三方服务器(即RunKeeper)上。 Also, without HealthKit permissions I would be allowed to store health data created by my app in iCloud. 另外,如果没有HealthKit权限,我将被允许在iCloud中存储由我的应用创建的健康数据。 If you take third party data from HealthKit and try to put that in iCloud, then you'll be rejected. 如果您从HealthKit中获取第三方数据并尝试将其放入iCloud中,那么您将被拒绝。

There's a couple questions in here, so I'll try to answer them in order. 这里有几个问题,所以我将尝试按顺序回答。

...how do I go about keeping my Core Data store and my HealthKit store in sync? ...如何使我的核心数据存储和HealthKit存储保持同步?

So there are two application modes you need to worry about for getting data updates: foreground and background. 因此,您需要担心两种应用程序模式以获取数据更新:前台和后台。

When in the foreground, you can utilize HKObserverQuery which provides a decent amount of flexibility in getting the data you need. 在前台时,您可以使用HKObserverQuery ,它在获取所需数据方面提供了相当大的灵活性。 The usual caveats apply when passing data across thread boundaries (as observer queries run on background queues). 当跨线程边界传递数据时,通常会出现警告(因为观察者查询在后台队列上运行)。 Pertinent docs: HKObserverQuery Docs 相关文档: HKObserver查询文档

In the background you have to register for background wakeup using enableBackgroundDeliveryForType(_:frequency:withCompletion:) . 在后台,您必须使用enableBackgroundDeliveryForType(_:frequency:withCompletion:)注册后台唤醒。 This will wake your application at (or close to) the specified frequency, at which point you'll need to jump through whatever necessary hoops to load your Core Data stack and do your updates. 这将以(或接近)指定的频率唤醒您的应用程序,这时您将需要跳过所有必要的循环来加载Core Data堆栈并进行更新。 Pertinent Docs: HKHealthStore Background Handling Docs 相关文档: HKHealthStore后台处理文档

...is the Core Data intermediary really necessary? ...核心数据中介真的必要吗?

No, and in fact using Core Data may be complete overkill for your uses. 不可以,实际上,使用Core Data可能对您的使用来说完全是矫kill过正。 I generally recommend against implementing Core Data at the outset of an application. 我通常建议不要在应用程序一开始就实现核心数据。 There are performance concerns, background wake concerns, schema migration concerns when you change schemas, and iCloud <> Core Data synchronization issues (most of which have been resolved as of iOS 9 but still crop up occasionally). 存在性能问题,背景唤醒问题,更改模式时的模式迁移问题以及iCloud <> Core Data同步问题(大多数问题已从iOS 9开始解决,但偶尔仍会出现)。

On top of all that, Apple's "template" for including Core Data in a new project generally doesn't follow best practice guidelines for integrating Core Data. 最重要的是,Apple在新项目中包含Core Data的“模板”通常不遵循集成Core Data的最佳实践准则。 Do some Google searches and take a look at Marcus Zarra's books on the subject. 进行一些Google搜索,然后看看Marcus Zarra关于该主题的书籍。

That all said, not using Core Data means having to write a bunch more code to enable iCloud to synchronize with your data store of choice, so it's tough to offer a suggestion as to the "correct" route to take. 综上所述, 使用Core Data意味着必须编写更多代码才能使iCloud与您选择的数据存储同步,因此很难就“正确”的使用方法提出建议。

I take this to mean that any Health data which was not created by your app cannot be stored in iCloud. 我的意思是,任何不是由您的应用创建的健康数据都无法存储在iCloud中。

Incorrect. 不正确 Take the line at face value. 以该线为准。 If Apple sees you've requested access to HealthKit and have provisioned iCloud access, you're probably going to get scrutinized and most likely rejected. 如果Apple看到您已请求访问HealthKit并配置了iCloud访问权限,则您可能会受到审查,很可能会被拒绝。 They are very touchy about user privacy in this regard, and correctly so in my opinion. 他们在这方面对用户隐私非常敏感,我认为正确。

Your note about Runkeeper, while accurate, is also flawed as Runkeeper (last I checked) does not use iCloud and so wouldn't be subject to this, so the analogy is flawed. 关于Runkeeper的说明虽然准确无误,但也存在缺陷,因为Runkeeper(我上次检查过)不使用iCloud,因此不受此限制,因此类推是有缺陷的。 Also, as per Apple's other guidelines, whatever other apps do or don't do has no bearing on your application's review status. 另外,根据Apple的其他指南,无论其他应用程序执行与不执行任何操作,都不会影响您的应用程序的审核状态。

In short, I would steer clear of storing users' health data in iCloud. 简而言之,我会避免将用户的健康数据存储在iCloud中。 Use another provider or your own server. 使用其他提供程序或您自己的服务器。

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

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