20

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.

sclv
  • 38,665
  • 7
  • 99
  • 204
newDeveloper
  • 1,365
  • 1
  • 17
  • 27
  • You can add an answer to your own question and accept it if you could fix your problem, since other people might have the same problem. – Besi Feb 28 '12 at 12:29

6 Answers6

58

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

IPhone Guy
  • 699
  • 7
  • 6
4

I just encountered this issue after changing the case of the name of the .xcdatamodeld file (e.g. refactored "mydatastore.xcdatamodeld" to "MyDatastore.xcdatamodeld") on a case-insensitive filesystem.

The fix includes both of the above answers:

  1. Refactor the data model file to a distinctly different name, e.g. "MyDatastoreTemporary.xcdatamodeld"

  2. Product > Clean

  3. Close XCode. Re-open XCode and the project.

  4. Refactor the data model file back to its final name, e.g. "MyDatastore.xcdatamodeld"

  5. Product > Clean

  6. Close XCode. Re-open XCode and the project.

Charney Kaye
  • 3,667
  • 6
  • 41
  • 54
  • I had this problem when Xcode inserted an underscore in the name of the model, while the product name contained a space in that position. I tried renaming and ended up here.. – Bjinse Feb 12 '14 at 16:21
1

I tried both of the above answers, however what fixed it for me was removing and re-adding the .xcdatamodeld file from/to the Xcode project. No cleaning was necessary. For some reason adding it back seems to fix the problem.

czechboy
  • 899
  • 7
  • 15
1

I resolved the problem moved to trash the model .xcdatamodeld and then added it one more time.

westrada
  • 597
  • 4
  • 8
  • can u please explain detail how u removed and added the .xcdatamodeld again,for me the .xcdatamodeld is not showing to delete and add again. – skkrish Aug 14 '15 at 06:11
  • This also did the trick for me. The problem was in .xcurentversion, where it was older name. – Nemanja Jan 03 '19 at 22:17
0

The by far safest way, if you have not yet versioned your model, is to do the following.

  1. Do not rename your xcdatamodeld file.
  2. Instead, create a brand new Data Model file with the new name.
  3. Keep the file empty and quit Xcode
  4. Open up a terminal and cd (change directory) into your old xcdatamodeld directory.
  5. Inside this directory should be a directory ending in xcdatamodel. Change into this directory.
  6. Inside the xcdatamodel directory you will find a file named contents. Copy this file into the same location under your newly named ?.xcdatamodeld/?.xcdatamodel directory.
  7. Open up Xcode and modify your Core Data code to access this new model.
  8. Build and test.
  9. Remove the old xcdatamodeld from your project.

This works with basic models that have no versions.

Bringo
  • 610
  • 6
  • 15
0

When I ran the project the first time, I specified the extension as xcdatamodeld. But that failed since, it should be momd. But I passed a nil instead of momd assuming extension won't be mandatory. But with nil, the initWithContentsOfURL failed and when I finally entered momd,it worked fine.

0x6d6e
  • 153
  • 2
  • 13