Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rockstarjoe

macrumors 6502a
Original poster
Jun 2, 2006
876
114
washington dc
Hi everyone,

I'm a noob. I'm working on an app that I would like to submit to the MAS. My app uses Core Data, and is a document based app (using NSPersistentDocument). The data store saves as a SQLite file within a package. I followed Apple's sample code to get the package working correctly.

Everything works great right now, and I am close to being ready to submit my app, but before doing so I have been trying to get everything sandboxed per the new app store / icloud rules.

I can't seem to find much info regarding how to handle an NSPersistentDocument based Core Data app, though. On the developer message board the keep saying that "documentation is forthcoming" but still nothing concrete.

Has anyone done something similar to this? If so, how did you handle it? I can't even seem to save out the SQLite file by itself, let alone in a package, even when I have turned on the read/write entitlements.

Thanks for any advice or documentation that you can point me towards.
 

rockstarjoe

macrumors 6502a
Original poster
Jun 2, 2006
876
114
washington dc
Sorry if my terminology is incorrect. In my document I override writeSafelytoURL: and create a NSFileWrapper. I then configure the persistentstorecoordinator for the URL that is passed in, and add it to the filewrapper. Then I write the filewrapper to disk.

In my project settings, I have added a document type with a special extension for my application's documents. I checked the box that says "Document is distributed as a bundle" and added "Core Data persistent store type = SQLite" as an additional document type property.

All of this functions correctly right now, so when I save I end up with a file that has file extension matching my document type, but this file is really a package (like an iphoto library file), so you can right-click on it to "Show Package Contents". Within the package file is the SQLite file, along with some other folders that I create.

I'm just not sure if it is even possible to do something like this within the restraints of the sandbox. Thanks for any insight.
 

JoshDC

macrumors regular
Apr 8, 2009
115
0
How are you trying to save it? If you're using NSPersistentDocument's save: method, you should be fine. If you're trying to save outside of the sandbox any other way, I think you're out of luck.
 

Kenndac

macrumors 6502
Jun 28, 2003
256
63
If you're using NSSavePanel or the standard save routines, you get access to exactly the path the user chose and and subpaths, so you should be able to create a directory there and do what you want.

Make sure you're not adding an extension or modifying the path in any way in code.
 

rockstarjoe

macrumors 6502a
Original poster
Jun 2, 2006
876
114
washington dc
If you're using NSSavePanel or the standard save routines, you get access to exactly the path the user chose and and subpaths, so you should be able to create a directory there and do what you want.

Make sure you're not adding an extension or modifying the path in any way in code.

Thanks everyone for the assistance. I think my problem is related to overriding the writeSafelyToURL: method. I'll do some experimenting and report back.
 

rockstarjoe

macrumors 6502a
Original poster
Jun 2, 2006
876
114
washington dc
After messing with this problem for the past few days I have found that NSPersistentDocument just does not play nice with sandboxing.

If someone is interested in seeing the problem in action, just create a new project and choose to use Core Data and Document based. Now turn on entitlements and code signing, and allow file system read / write. Finally, in your document types, delete everything except SQL. Then run the project. Save the document. Then try to save it again. You will most likely get an error. This can be solved by overriding the save method and creating a folder at the save destination, and then saving the document inside of this folder.

I think the problem is related to the fact that when you save the document as a SQL type, it also tries to save the journaled sql file temporarily in the same location, and I think the sandbox blocks it. I don't know for sure if this is exactly what is going on but that is my hunch.

Anyway, I've gone back to my original plan and am saving the document within a bundle. Just thought I'd post this in case anyone else is having similar difficulties.
 

indiekiduk

macrumors 6502
Jul 26, 2005
477
408
Glasgow, Scotland
... The data store saves as a SQLite file within a package. I followed Apple's sample code to get the package working correctly.
I would love to see this sample code of a persistent document with file wrapper, do you have a link?

According to the docs, NSPersistentDocument doesn't support file wrappers. I've seen others attempt to use a subclass of NSDocument instead, with a file wrapper that has their own Core Data stack inside it. But that seems a lot of work and error-prone because e.g. it has to detect and respond to file moves. Which btw is implemented by NSPersistentDocument by overriding setFileURL (I had a look using HopperApp).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.