简体   繁体   English

带有核心数据和文件同步的iCloud

[英]iCloud with Core Data and File sync

I'm working on an Application that uses Core Data with iCloud (with the great improvement given by iOS7). 我正在开发一个将核心数据与iCloud一起使用的应用程序(iOS7给出了很大的改进)。 This application stores data to describe a task with this information: 此应用程序存储数据以使用此信息描述任务:

  • name a NSString name一个NSString
  • date a NSDate date NSDate
  • image a NSString which describes a path to the picture imageNSString其描述将图片的路径

The pictures could be stored in Documents or Library Directory (I have to decide which is the more convenient folder), by the way, in the same folder with a unique name. 顺便说一句,图片可以存储在文档或图书馆目录中(我必须决定哪个是更方便的文件夹)在具有唯一名称的同一文件夹中。

Now I'd like to activate iCloud sync for the images too otherwise the experience of the user will be incomplete (I just sync DB data.. no images, a strange/wrong behaviour for an app). 现在我想为图像激活iCloud同步,否则用户的体验将不完整(我只是同步数据库数据..没有图像,应用程序的奇怪/错误行为)。

I'm really confused by Apple Documentation. 我对Apple文档感到困惑。 I can't find a way to understand exactly how iCloud data works for this kind of needs. 我无法找到一种方法来准确理解iCloud数据如何满足这种需求。 I just want to sync every file of a folder as soon as they will be created. 我只想在创建文件夹时立即同步它们的每个文件。 So my questions are: 所以我的问题是:

  • Could you share some good resources to learn how to use iCloud for file sync 您可以分享一些很好的资源来学习如何使用iCloud进行文件同步
  • Have I to use UIDocument and other iCloud API? 我是否要使用UIDocument和其他iCloud API? or is there something "automatic". 还是有“自动”的东西。 Quite a new bye/stupid question, I know :( 相当新的再见/愚蠢的问题,我知道:(
  • Are there any problem using Core Data and Document based iCloud synch in the same app? 在同一个应用程序中使用基于Core Data和Document的iCloud synch是否有任何问题?

Note: I know that I can sync data just by adding file in the document folder and hoping that users activate document sync... but this is not what I want obviously. 注意:我知道我可以通过在文档文件夹中添加文件来同步数据,并希望用户激活文档同步...但这显然不是我想要的。

It is pretty straight forward to use both Core Data transaction log synchronisation and file based synchronisation in the same app to achieve what you want to achieve. 在同一个应用程序中同时使用Core Data事务日志同步和基于文件的同步来实现您想要实现的目标非常简单。

So you would set up your Core Data stack to use iCloud options and synchronise data changes via iCloud. 因此,您需要设置Core Data堆栈以使用iCloud选项并通过iCloud同步数据更改。 At the same time you would store your images in the Apps iCloud container so they get synchronised as well. 同时,您可以将图像存储在Apps iCloud容器中,以便它们同步。 Just remember you need to use a relative reference to the images in your Core Data fileURL because the full pathname will vary depending on the device the app is running on. 请记住,您需要对Core Data fileURL中的图像使用相对引用,因为完整路径名将根据运行应用程序的设备而有所不同。 So for example you would just store the image filename in Core Data and use a standard directory such as 'iCloudContainer/Documents/Images/' to store them. 因此,例如,您只需将图像文件名存储在Core Data中,并使用标准目录(如“iCloudContainer / Documents / Images /”)来存储它们。 'iCloudContainer' being the URL you get by calling the [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:] API. 'iCloudContainer'是您通过调用[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:] API获得的URL。

I have uploaded sample Core Data apps that use iCloud for transaction log synchronisation (ie synchronising data in Core Data) and that use file synchronisation for storing Core Data backup files in iCloud which can then be accessed by any device. 我上传了使用iCloud进行事务日志同步(即同步Core Data中的数据)的示例Core Data应用程序,以及使用文件同步在iCloud中存储Core Data备份文件,然后可以由任何设备访问。 You should be able to use the same code for moving backup files to and from iCloud for your images. 您应该能够使用相同的代码将iRoud的备份文件移动到iCloud或从iCloud移动备份文件。 Just remember you have to trigger download of files from iCloud before you can use them either by doing a coordinated read or by initialising the download using NSFileManager. 请记住,您必须先触发从iCloud下载文件,然后才能通过协调读取或使用NSFileManager初始化下载来使用它们。

http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/ http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/

Download and run the sample apps and use the built in Backup File Manager to make backup files and to copy them to and from iCloud using different devices. 下载并运行示例应用程序,并使用内置的备份文件管理器制作备份文件,并使用不同的设备将它们复制到iCloud和从iCloud复制。 Then just use the same code when storing your image files. 然后在存储图像文件时使用相同的代码。

Your App does have to handle things like the user changing iCloud account, logging into or out of iCloud etc. and them move the core data file and image files accordingly. 您的应用程序必须处理用户更改iCloud帐户,登录或退出iCloud等内容,并相应地移动核心数据文件和图像文件。

The only way to have this happen automatically is to create a binary data attribute in your model for the images. 自动发生这种情况的唯一方法是在模型中为图像创建二进制数据属性。 If you do this, you will probably want to check the external binary storage allowed option, so the photos end up stored as files and not in the database. 如果这样做,您可能需要检查外部二进制存储允许选项,因此照片最终存储为文件而不是数据库。

If you would rather store the photos external to your store, you will have to do more work. 如果您希望将照片存储在商店外部,则必须完成更多工作。 You will need to migrate the photos into the iCloud container yourself, using the NSFileManager methods, for example. 例如,您需要使用NSFileManager方法自己将照片迁移到iCloud容器中。 You could also use a class like iCloud Access if you find that easier. 如果您觉得更容易,也可以使用像iCloud Access这样的类。

The downside to handling the photos yourself is that you can never be sure that they have all arrived on your device when the Core Data store syncs, so it could be one or more photos are missing, even though there are entries for them in the store. 自己处理照片的缺点是,当核心数据存储同步时,你永远无法确定它们是否已全部到达你的设备,因此它可能会丢失一张或多张照片,即使它们在商店中有条目。 You would have to make sure your app could handle this scenario, perhaps showing a placeholder image until the real photo was accessible. 您必须确保您的应用可以处理此方案,可能会显示占位符图像,直到可以访问真实照片。

There are no issues using Core Data and Document syncing in one app. 在一个应用程序中使用Core Data和Document同步没有问题。 In fact, they are exactly the same under the covers. 事实上,它们在封面下完全相同。 From iClouds point of view, they are all just files to be transferred. 从iClouds的角度来看,它们都只是要传输的文件。

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

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