1
votes

I'm having a hard time debugging a problem with my data (CoreData, NSPersistentDocument).

I have a subclass of NSPersistentDocument. I am using NSManagedObject subclasses / standard Core Data models. I'm not doing anything special in NSPersistentDocument or with the NSManagedObject classes. I am merely creating an object (in NSPersistentDocument's subclass):

[NSEntityDescription insertNewObjectForEntityForName:@"ModelName" 
    inManagedObjectContext:[self managedObjectContext]];

When I attempt to save the document in my app, there is a drop-down for file formats. It includes Binary (default), SQLite, and XML. I save the file as XML. When I try to view it (using less, or even opening in Finder), I find that the file is stored as binary.

Is there something special I need to go to force it to XML?

My understanding based on the documentation from Apple is that in using an NSPersistentDocument subclass, I don't need to do the work of setting up the NSPersistentStore or NSPersistentStoreCoordinator. My understanding is all of this comes for free. Also from what I've read, XML is the default.

1
If you're curious about the question that got me here: stackoverflow.com/questions/9405120/…Tyler A.
FWIW, saving as SQLite does save to the correct format.Tyler A.

1 Answers

0
votes

The template that Xcode creates for a document based app with Core Data works perfectly for what you're describing. You may need to include a bit more info, but one thing to check would be that the document types in your Info.plist are correct. Below are the out of the box values. There's also a graphical editor in Xcode for this under the Info tab when you have your target selected in the project view.

Try creating a new project with Core Data and Document Based Application checked (Xcode 4.3) and check to see if that works fine. If it does, then something in your configuration has changed to make it binary instead of XML.

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>binary</string>
        </array>
        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>application/octet-stream</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>Binary</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSTypeIsPackage</key>
        <false/>
        <key>NSDocumentClass</key>
        <string>Document</string>
        <key>NSPersistentStoreTypeKey</key>
        <string>Binary</string>
    </dict>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>sqlite</string>
        </array>
        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>application/octet-stream</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>SQLite</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSTypeIsPackage</key>
        <false/>
        <key>NSDocumentClass</key>
        <string>Document</string>
        <key>NSPersistentStoreTypeKey</key>
        <string>SQLite</string>
    </dict>
    <dict>
        <key>CFBundleTypeExtensions</key>
        <array>
            <string>xml</string>
        </array>
        <key>CFBundleTypeIconFile</key>
        <string></string>
        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>text/xml</string>
        </array>
        <key>CFBundleTypeName</key>
        <string>XML</string>
        <key>CFBundleTypeOSTypes</key>
        <array>
            <string>????</string>
        </array>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSTypeIsPackage</key>
        <false/>
        <key>NSDocumentClass</key>
        <string>Document</string>
        <key>NSPersistentStoreTypeKey</key>
        <string>XML</string>
    </dict>
</array>