iCloud与核心数据和文件同步 [英] iCloud with Core Data and File sync

查看:140
本文介绍了iCloud与核心数据和文件同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个使用Core Data与iCloud的应用程序(与iOS7给出的巨大改进)。此应用程序存储数据以描述具有此信息的任务:




  • name code> NSString

  • date a NSDate

  • image a NSString



图片可以存储在文档或库目录(我必须决定哪个是更方便的文件夹)



现在我想为图片激活iCloud同步,否则用户的体验将不完整(我只是同步DB数据..没有图像,一个应用程序的奇怪/错误的行为。)



我真的很困惑的苹果文档。我找不到一个方式来了解iCloud数据如何工作的这种需要。我只想在文件夹的每个文件创建时立即同步。所以我的问题是:




  • 您可以分享一些好的资源,了解如何使用iCloud进行文件同步

  • 我要使用UIDocument和其他iCloud API吗?或有什么自动。我知道:(

  • 在同一个应用程序中使用Core Data和基于文档的iCloud同步时是否有问题?



    • 注意:我知道我可以通过在文档文件夹中添加文件来同步数据,并希望用户激活文档同步...但这不是我想要的。 / p>

      解决方案

      这是很直接的使用Core Data事务日志同步和基于文件的同步在同一个应用程序来实现你想要的



      因此,您将设置您的Core Data堆栈以使用iCloud选项,并通过iCloud同步数据更改。同时,您将图像存储在应用程序iCloud容器,所以他们得到同步为了记住你需要使用一个相对于你的核心数据fileURL中的图像引用,因为完整的路径名将根据应用程序运行的设备而有所不同,例如你只是存储映像文件名,并使用标准目录(如iCloudContainer / Documents / Images /)来存储它们。 'iCloudContainer'是通过调用 [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:] API获得的URL。



      已上传使用iCloud进行事务日志同步(即在Core Data中同步数据)并使用文件同步在iCloud中存储Core Data备份文件,然后可由任何设备访问的示例Core Data应用程序。您应该能够使用相同的代码为iCloud移动备份文件到您的图像。只要记住,您必须从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/sample-apps-explanations/



      下载并运行示例应用程序,并使用内置的备份文件管理器制作备份文件,并使用不同的设备将其复制到iCloud或从iCloud复制。然后只需在存储图像文件时使用相同的代码。



      您的应用程序必须处理像更改iCloud帐户,登录或注销iCloud等用户,并相应移动核心数据文件和图像文件。 / p>

      I'm working on an Application that uses Core Data with iCloud (with the great improvement given by iOS7). This application stores data to describe a task with this information:

      • name a NSString
      • date a NSDate
      • image a NSString which describes a path to the picture

      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).

      I'm really confused by Apple Documentation. I can't find a way to understand exactly how iCloud data works for this kind of needs. 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
      • Have I to use UIDocument and other 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?

      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.

      So you would set up your Core Data stack to use iCloud options and synchronise data changes via iCloud. At the same time you would store your images in the Apps iCloud container so they get synchronised as well. 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. 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. 'iCloudContainer' being the URL you get by calling the [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:] API.

      I have uploaded sample Core Data apps that use iCloud for transaction log synchronisation (i.e. 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. You should be able to use the same code for moving backup files to and from iCloud for your images. 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.

      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/

      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. 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与核心数据和文件同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆