NSPersistentDocument, Core Data, and Sandboxing

Discussion in 'Mac Programming' started by rockstarjoe, Nov 21, 2011.

  1. rockstarjoe macrumors 6502a

    rockstarjoe

    Joined:
    Jun 2, 2006
    Location:
    washington dc
    #1
    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.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    What do you mean by a package? A .pkg file? A directory with the Bundle bit set?
     
  3. rockstarjoe thread starter macrumors 6502a

    rockstarjoe

    Joined:
    Jun 2, 2006
    Location:
    washington dc
    #3
    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.
     
  4. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #4
    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.
     
  5. Kenndac macrumors 6502

    Kenndac

    Joined:
    Jun 28, 2003
    #5
    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.
     
  6. rockstarjoe thread starter macrumors 6502a

    rockstarjoe

    Joined:
    Jun 2, 2006
    Location:
    washington dc
    #6
    Thanks everyone for the assistance. I think my problem is related to overriding the writeSafelyToURL: method. I'll do some experimenting and report back.
     
  7. rockstarjoe thread starter macrumors 6502a

    rockstarjoe

    Joined:
    Jun 2, 2006
    Location:
    washington dc
    #7
    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.
     

Share This Page