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

mduser63

macrumors 68040
Original poster
Nov 9, 2004
3,042
31
Salt Lake City, UT
I've got a document-based Core Data app that was previously working, but is now broken. I can't figure out what I did to break it, but the symptom is that saving doesn't work. I can fill up the document with entities and hit save and it will save a file out, but the window doesn't change to reflect that it is displaying a saved file, it still has "Untitled" in the title bar and the close button shows that there are unsaved changes, even immediately after saving. If I save as XML, I get something like this in the XML file:

<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "file:///System/Library/DTDs/CoreData.dtd">

<database>
<databaseInfo>
<version>134481920</version>
<UUID>C6303AF0-47D9-483B-AF0E-588E488C22E1</UUID>
<nextObjectID>101</nextObjectID>
<metadata></metadata>
</databaseInfo>
</database>

There should be entities with multiple attributes and relationships, but instead, nothing. If I open one of these files, my app doesn't complain it just opens up what appears to be a completely empty document. It doesn't matter whether I save in Binary, SQL or XML, the same thing happens.

I haven't purposely done anything to modify or control the save functionality in my application. I'm relying on Core Data and the defaults set up in the Core Data Document-Based app template. However, obviously something I've done has caused a problem. I haven't changed the default document types for my application. I've tried deleting everything relating to my app, including the cache and .plist file and rebuilding it with no effect. I haven't been able to find anyone reporting this same problem by searching, so I'm hoping someone here has seen it or has some idea why it may be happening.

Thanks,
Andrew Madsen
 

mduser63

macrumors 68040
Original poster
Nov 9, 2004
3,042
31
Salt Lake City, UT
Replying to myself here...

OK, so I've figured out a little more, but I'm still confused. I thought that maybe the Managed Document Contexts for MyDocument and the NSObjectController/NSArrayControllers I'm using were different. Sure enough, some debug code showed that MyDocument's managed object context is empty even when the table in the document is full of entries. Displaying the objects in the managed object document for one of the array controllers shows that it is full of the objects one would expect. The managedObjectContext for the controllers are all bound to managedObjectContext in File's Owner (My Document). If I change them so they're bound to the instance of My Document as opposed to File's Owner (My Document), saving works. That is, I can save and open the XML file and all the objects are there. However, if I open the saved file, everything shows up again, and debugging reveals that the entities in the XML file are not getting loaded into the managed object context in MyDocument (or the controllers). Here's the weird part though. If I switch the managedObjectContext bindings on the controllers back to File's Owner (My Document), saving stops working again (as expected), but opening an XML file written correctly (with the bindings on MyDocument) works.

I wish I had a better grasp of this stuff. It seems like the XML file's entities are getting loaded into File's Owner's MOC, but somehow the actual MyDocument instance doesn't get the same MOC. All of this indicates that the File's Owner's MOC and the MyDocument instance's MOC are not the same like (I think) they should be. I haven't purposefully done anything to make this happen, so I'm having a really hard time figuring out what's causing it.

EDIT: I suppose you're not supposed to have a separate instance of MyDocument in the nib file. I have no idea why I added that then started connecting things to it. Anyway, the solution is to connect everything to File's Owner, otherwise you've got two instances of MyDocument "fighting" with each other.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.