On 2008 Dec, 24, at 0:03, mmalc Crawford wrote:

<http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSPersistentDocument_Class/Reference/Reference.html#//apple_ref/doc/uid/TP30001179-CJBJBIHA >


Thank you, mmalc. Indeed it was explained further down in the documentation you noted.

I believe the problem is my triggering some kind of bug in the way Xcode handles multiple/versioned data models.

My project included two xcdatamodel files. One was versioned and one was not. The problem apparently occurred when I wanted to consolidate them into one. Hoping to avoid re-typing all the attributes, using Xcode's GUI data model editor, I copied the single entity which was in the non-versioned xcdatamodel to the clipboard and pasted it into the versioned xcdatamodel, saved it, and deleted the non-versioned xcdatamodel. Everyone looked happy.

Except, when the project built, a very strange thing would happen. In Contents/Resources there would be a folder named Bkmm.momd containing an item named BmxBk 2.mom. Bkmm was the name of the non-versioned xcdatamodel which I had deleted. BmxBk 2 was version 2 of the consolidated xcdatamodel which remained.

Then when the project ran, my persistent document initialized itself with an invalid managed object model. I proved that by gutting my NSPersistentDocument subclass and replacing it with a Bonehead implementation [1] that simply logs each object in the persistence stack when the user creates a new document. The console output [2] shows that the moc and the psc are OK, but the mom is invalid.

First, I tried adding another valid data model containing a simple entity with one attribute to the project. Still same crash.

Then, I removed the screwed-up data model from the project and re- added it. Now, it even showed up in wrong in Xcode's Groups & Files, with the BmxBk.xcdatamodel contained in a "folder" named after the deleted Bkmm.xcdatamodel...

<<inline: XCDM.png>>



If I deleted all xcdatamodels from the project, then it would build run and log the whole persistence stack without crashing. Of course, the model's entities logged as an empty dictionary.

Finally, I created a new Cocoa Core Data Document-Based application project from scratch, replaced the data model provided by the Xcode template with a copy of my consolidated xcdatamodel, and added the iniWithType:error: implementation from my Bonehead implementation to the template's MyDocument. Building and running this project logs my entire managed object model as expected; no crash.

So, tomorrow I'm going to copy all of my old files to the new project and I expect that it will work again. Oh well, the project was in need of a renaming and a housecleaning anyhow.

Unless someone sees otherwise, I'll strip down my old project someday and file a bug regarding Xcode.

Jerry


[1] Bonehead implementation of an NSPersistentDocument subclass

@interface BmxBk : NSPersistentDocument {
}

@implementation BmxBk

- (id)initWithType:(NSString *)type error:(NSError **)error_ {
    self = [super initWithType:type
                         error:error_];
    if (self) {
// All of the code in this method executes OK if I paste it into // -[MyDocument initWithType:error] of the DepartmentsAndEmployees
        // Apple Sample Code.

// The usleeps are because the NSLogs sometimes appear after the later // gdb startup in the console log, obscuring what triggered the crash.

        // Do we have a valid doc type?
        NSLog(@"doc type = %@", type) ;

        // Do we have a valid moc?
        NSManagedObjectContext* moc = [self managedObjectContext] ;
        NSLog(@"moc = %@", moc) ;
        NSLog(@"Will sleep 5") ;
        usleep (5000000) ;

        // Do we have a valid psc?
NSPersistentStoreCoordinator* psc = [moc persistentStoreCoordinator] ;
        NSLog(@"psc = %@", psc) ;
        NSLog(@"Will sleep another 5") ;
        usleep (5000000) ;

        // Do we have a valid mom?
        NSManagedObjectModel* mom = [psc managedObjectModel] ;
NSLog(@"mom = %@", mom) ; // <-- Crash or log something not a mom.
        NSLog(@"Will sleep a third 5") ;
        usleep (5000000) ;

        // Can we see the entities in our mom?
        NSDictionary* entities = [mom entitiesByName] ;
        NSLog(@"entities = %@", entities) ;
        NSLog(@"Will sleep a fourth 5") ;
        usleep (5000000) ;
    }
    return self ;
}

@end

[2] Console output produced by [1]

• User clicks in menu File > New.  Note that mom is not a mom.
MyApp[24942:10b] doc type = MyApp Document
MyApp[24942:10b] moc = <NSManagedObjectContext: 0x15b19b40>
MyApp[24942:10b] psc = <NSPersistentStoreCoordinator: 0x15b15cb0>
MyApp[24942:10b] mom = <NSKeyValueUndefinedSetter: 0x15b168b0>
MyApp[24942:10b] *** -[NSKeyValueUndefinedSetter entitiesByName]: unrecognized selector sent to instance 0x15b168b0 MyApp[24942:10b] *** -[NSKeyValueUndefinedSetter entitiesByName]: unrecognized selector sent to instance 0x15b168b0
• User clicks in menu File > New again.  This time, mom is just invalid.
MyApp[24942:10b] doc type = MyApp Document
MyApp[24942:10b] moc = <NSManagedObjectContext: 0x2e5290>
MyApp[24942:10b] psc = <NSPersistentStoreCoordinator: 0x2f1780>

[Session started at 2008-12-25 18:43:28 -0800.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to