My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
CoreDataCaveat  
Integration in CoreData applications on Tiger.
Phase-Implementation, Featured
Updated Feb 5, 2010 by pierre.b...@gmail.com

Introduction

When linking against Tiger SDK, CoreData applications using the iMedia framework may fail to read their own documents. The error is:

Error Domain=NSCocoaErrorDomain Code=134020 UserInfo=0x2b7b10 "The model configuration used to open the store is incompatible with the one that was used to create the store."
NSUnderlyingException = Can't find table for entity MovieReference in database at URL ...

The entity name may vary. It is a random name from the iMedia movie cache model.

The stack is:

#0	0x96da7e17 in objc_exception_throw
#1	0x9496344b in -[NSSQLCore _ensureDatabaseMatchesModel]
#2	0x94962b35 in -[NSSQLCore load:]
#3	0x94958732 in -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]
#4	0x915f5fe9 in -[NSPersistentDocument configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:]
#5	0x915f6fab in -[NSPersistentDocument(NSPersistentDocumentDeprecated) configurePersistentStoreCoordinatorForURL:ofType:error:]
#6	0x915f63b6 in -[NSPersistentDocument readFromURL:ofType:error:]
#7	0x000041c2 in -[Document readFromURL:ofType:error:] at Document.m:177
#8	0x913f16c6 in -[NSDocument initWithContentsOfURL:ofType:error:]
#9	0x913c1274 in -[NSDocumentController makeDocumentWithContentsOfURL:ofType:error:]
#10	0x913c0898 in -[NSDocumentController openDocumentWithContentsOfURL:display:error:]

Details

There was a change between Tiger and Leopard in the way NSPersistentDocument loaded its model.

If you have a document created on Tiger, the model is created using mergedModelFromBundles: [NSBundle allBundles], and on Leopard, with mergedModelFromBundles: nil. The problem with the first version is, as you've seen, as more and more frameworks start to include bundles, the model gets polluted with entities from things you don't care about.

There's a check done at runtime to see which version you were compiled on, so you get the same runtime behavior for applications that were compiled and linked on Tiger that depended on the original behavior, but if you recompile on Leopard, you get the later, safer behavior.

Workaround

The idea is to force the NSDocument subclass of the host application to use only the model it is build upon. In the below example, this model is named "Document". The workaround is enabled by adding the below code to the Document class:

Adding the following method to my Document class fixed the problem:

- (id)managedObjectModel
{
    static id sharedModel = nil;
	
    if (sharedModel == nil) {
		NSBundle *bundle = [NSBundle bundleForClass:[self class]];
		NSString *path = [bundle pathForResource:@"Document" ofType:@"mom"];
		NSURL *url = [NSURL fileURLWithPath:path];
		NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
		
		sharedModel = model;
    }
	
    return sharedModel;
}

Sign in to add a comment
Powered by Google Project Hosting