3

I would like to create a NSPersistentDocument in Mac OS X and read this document as a UIManagedDocument on iOS 7.

Is this possible?

Are both file formats compatible?

Thank you!

AlexR
  • 5,514
  • 9
  • 75
  • 130

2 Answers2

2

Interesting question - I can confirm that the basic core data files are compatible. I have a Mac app and an iOS app using the same file that gets synced using iCloud. The app is a document based app and currently I have been storing the actual database file in iCloud so the whole file gets sync'ed by iCloud.

This works fine but obviously if a user opens the file on two devices and is not careful about saving and closing there is a possibility their changes may be overridden.

Apple has approved the Mac app which uses standard NSPersistentDocument to create and save files. Unfortunately they have rejected the iOS apps with some obscure reference to not conforming to their data storage guidelines, saying that documents must be stored in /Documents directory if they need to be backed up to iCloud. Well if the user has selected iCloud then I store the files in the iCloud location provided by the API calls.

Anyway I am still waiting to hear back from them about what is specifically wrong with this approach since it seems to be the same one used by Pages and other document based apps.

If I try using UIManagedDocument then iOS creates a folder structure and stores the database inside this folder structure. The Mac app File->Open dialog then shows this folder structure as well as a file that essentially looks like a the normal sqlite file. But then perhaps OS X 10.8.4 does not implement the latest iCloud/Core Data stuff - who knows...

EDIT Here is a link to code examples and videos showing OSX and iOS app integration using Core Data and iCloud. http://ossh.com.au/design-and-technology/software-development/uimanageddocument-icloud-integration/

Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
  • Both your iOS and OS X app should store iCloud documents in `/Documents` you are most likely storing your files at the root level of the container as that is the path you get back by default. Anything at that level can not be deleted with as fine a touch as items in the /Documents level (either everything or nothing). If you are creating a "shoebox" app that has no documents just one big data store it gets stored at the root level of the container. – theMikeSwan Jan 04 '14 at 09:09
2

I agree this is definitely possible. I'm using NSPersistent document on OS X 10.8/10.9/10.10 with a binary core data format (no wrappers - plain files). On iOS i'm using UIDocument. Core data works fine in both environments.

Apple says NSPersistentDocument does not support iCloud. It is more correct to say it isn't fully supported. Most of the NSDocument support (which includes iCloud Document Library access from 10.8) will work. Handling of conflicted files on open works.

You can't enable auto save (which is listed as a requirement for iCloud Documents in the iCloud Design Guide). Autosave works asynchronously and is definitely not supported by NSPersistentDocument.

So if you handle file saving, and conflicts, it is possible to use NSPersistent document. There are some quirks: so, for example, if an iCloud change arrives on OS X for a document that is open, the normal NSDocument response would be to automatically reopen the document. This doesn't happen - and there is no warning the file has changed until you are about to save the file. But at least there is a warning. On iOS it is easier to detect changes as they happen by using UIDocumentStateChangedNotification.

MichaelR
  • 1,681
  • 15
  • 28
  • can you distribute NSPersistentDocument and UIManagedDocument sample projects interacting on the same Core Data documents? – Tobe Mar 13 '19 at 10:38
  • Hi @Tobe. I'm sorry it isn't easy to give you a sample project. The best advice I can give is to read [link] (https://developer.apple.com/library/archive/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/AdvancedTopics/AdvancedTopics.html). I needed to subclass `NSDocument` and override `initWithType`, `readFromURL`, `saveDocument`, `saveDocumentWithDelegate`, `close` and `makeWindowControllers`. Once you start creating documents and introducing conflicts with a well instrumented app, the path will become clearer. iOS is straightforward in comparison. – MichaelR Mar 14 '19 at 23:47
  • Hi @MichaelRourke, thank you for the interesting info. Is the solution you propose still the best in 2020 to sync core data documents between macOS and iOS via iCloud Drive? Thanks! – vomi Mar 07 '20 at 14:34
  • hi @vomi. I'm not certain it is the best solution. It works but relys on extensive testing to ensure the various edge cases are satisfied. Also using the atomic data store means you can't support independent updates to records. If I was starting afresh I would look at Cloudkit and try to devise a true cross platform solution. My application has a low rate of changes, so your use case will drive the best design. – MichaelR Mar 08 '20 at 20:22