简体   繁体   English

在iCloud中使用CoreData时,iCloud是否会同步所有CoreData数据

[英]Will iCloud sync all CoreData data when using CoreData in iCloud

I have an application that I want to add iCloud support to. 我有一个应用程序,我想添加iCloud支持。 This app loads data from a server, and the data is stored in CoreData so that NSFetchedResultsController s can manage the UITableView s. 这个应用程序加载从服务器的数据,并将数据存储在CoreData使NSFetchedResultsController S可管理UITableView秒。

Almost all the data stored is deleted when the app is terminated, because the data is rather time sensitive and it does not make sense to store it. 当应用程序终止时,几乎所有存储的数据都会被删除,因为数据对时间非常敏感,而且存储它是没有意义的。 There are however a few items in CoreData that are saved, such as the order of certain items, and user selected favorites. 但是,CoreData中有一些项目已保存,例如某些项目的顺序和用户选择的收藏夹。

Would it make sense to use Core Data in iCloud to sync these few saved items across devices? 在iCloud中使用Core Data来跨设备同步这几个保存的项目是否有意义? My concern is that once all the table views are populated with the temporary data, then the device will use bandwidth syncing this information when that is unwanted. 我担心的是,一旦所有表视图都填充了临时数据,那么当不需要时,设备将使用带宽同步这些信息。

Yes, it syncs all of the data in the store. 是的,它同步商店中的所有数据。 But there's more to the story than that. 故事还不止于此。

You tell Core Data to use iCloud when you add the persistent store to the persistent store coordinator-- by using the right option values when calling addPersistentStoreWithType:configuration:URL:options:error: . 当您将持久存储添加到持久性存储协调器时,您可以告诉Core Data使用iCloud - 在调用addPersistentStoreWithType:configuration:URL:options:error:时使用正确的选项值addPersistentStoreWithType:configuration:URL:options:error: . If you tell Core Data to use iCloud here, all of the data in that persistent store will be synced. 如果您告诉Core Data在此处使用iCloud,则将同步该持久性存储中的所有数据。

However, you can call this method more than once, using different persistent store files. 但是,您可以使用不同的持久存储文件多次调用此方法。 It's called a persistent store coordinator because it can coordinate among more than one persistent store. 它被称为持久性存储协调器,因为它可以在多个持久性存储之间进行协调。 Most apps use only one, but you can use as many as you need. 大多数应用只使用一个,但您可以根据需要使用多个应用。

There are a couple of different ways to deal with this, depending on how your app works. 根据您的应用的工作方式,有几种不同的方法可以解决这个问题。

  • If you use the same Core Data entity types in both persistent stores (ie synced and non-synced data all uses the same entities), you'll need to tell your managed object context which store it should use for each new object. 如果在两个持久性存储中使用相同的Core Data实体类型(即同步和非同步数据都使用相同的实体),则需要告知您的托管对象上下文应该为每个新对象使用哪个存储。 You do this by calling assignObject:toPersistentStore: on the context when creating the object. 您可以通过在创建对象时在上下文中调用assignObject:toPersistentStore:来完成此操作。
  • If you use different entities for different stores (ie synced data uses one subset of your data model, non-synced data uses a different subset), you can use different configurations in the data model, and the store will be chosen automatically. 如果对不同的存储使用不同的实体(即同步数据使用数据模型的一个子集,非同步数据使用不同的子集),则可以在数据模型中使用不同的配置,并自动选择存储。 Each configuration effectively defines a named subset of the model that contains only some of the entities. 每个配置都有效地定义了仅包含一些实体的模型的命名子集。 Use the right configuration name when adding the persistent store, and new objects will go to the appropriate store. 添加持久性存储时使用正确的配置名称,新对象将转到相应的存储。

If the data that shouldn't sync is really transient, consider using NSInMemoryStoreType when setting up the non-syncing persistent store. 如果不同步的数据实际上是瞬态的,请考虑在设置非同步持久性存储时使用NSInMemoryStoreType That uses more memory, but it also saves you from needing to delete the store contents. 这使用了更多的内存,但它也使您无需删除商店内容。 When the app exists, an in-memory store just disappears. 当应用程序存在时,内存存储就会消失。 If you can spare the memory, this can be a lot more convenient to use. 如果你可以节省内存,这可以更方便使用。 They work pretty much the same (you can still use NSFetchedResultsController , for example) but they never get saved to a file. 它们的工作方式基本相同(例如,您仍然可以使用NSFetchedResultsController ),但它们永远不会保存到文件中。

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

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