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?
source share