NSManagedObjectModel initWithContentsOfURL returns nil eventhough the modelURL is valid

newDeveloper picture newDeveloper · Jul 19, 2011 · Viewed 8.8k times · Source

my NSManagedObjectModel is returning nil eventhough the path is correct.

        NSString *modelKey = [NSString stringWithFormat:@"/%@/Model", name];
    NSString *modelPath = [((Configuration *)[Configuration shared]) stringEntry:modelKey];
    NSURL *modelURL = nil;
    if ( ! [modelPath contains:@"://"] ) {
        modelPath = PathForBundleResource( modelPath );
        modelURL = [NSURL fileURLWithPath:modelPath];
    }
    NSManagedObjectModel *m = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSString *PathForBundleResource(NSString *relativePath)

    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:relativePath];

I've reset the simulator, did a clean build but nothing helped.

I'm new to iPhone programming (core data in particular).

Would appreciate any help.

Edit: I've edited the original post regarding the xcdatamodeld file. It wasn't linked up properly initially but now it is and still having the same problem.

Edit 2: Apparently the name of the xcdatamodel had some variations to the name of the xcdatamodel after linkage. It's working now. Feeling so stupid. Don't know how to delete this question.

Answer

IPhone Guy picture IPhone Guy · Apr 28, 2012

I had the same problem after I renamed .xcdatamodeld file. The problem was solved by closing and reopening the Xcode client.