Basic data: the model is zero, and modelURL is valid

__managedObjectModel exists nil even modelURL. There is a similar message , but the accepted answer (rename the model file and restart Xcode) does not work for me.

 - (NSManagedObjectModel *)managedObjectModel { if (__managedObjectModel != nil) { return __managedObjectModel; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Failed" withExtension:@"momd"]; __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return __managedObjectModel; } 

I po modelURL in console

 (lldb) po modelURL (NSURL *) $4 = 0x088832f0 file://localhost/Users/philip/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/9E59167C-8D9E-4ADE-BBD7-0BE9A33A6A86/Failed.app/Failed.momd/ 
+4
source share
1 answer

I solved the problem after 3 hours. Finally. The solution is simple: just use the following code

 __managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; 

instead

 NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Failed" withExtension:@"momd"]; __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 

The reason is that I once created a new model file (.xcodemodeld) and deleted the old one. And the two model files have different names. In fact, the old model file is NOT deleted at all. It is still in the main application bundle.

I check the iphone simulator directory and surprisingly I see two compiled model files (.momd) that exist! I tried to remove the old mom. But every time my application starts, the old mamd appears. I proceed to the construction phase of the target task and make sure that the old model file is not in compilation sources. So strange..

Since there are several compiled model files in the main package, they must be combined. This is why mergedModelFromBundles: comes into play instead of a single modelURL .

If you never delete any model file, using single modelURL should not be a problem.

Although the problem is resolved, I donโ€™t understand why the simulator stores all the files of the remote model in the main package. That doesn't make sense to me. Anyone explain?

+8
source

All Articles