Core Data model files does not load on rename

前端 未结 6 948
眼角桃花
眼角桃花 2020-12-29 09:52

I have a model file thats named \"Model\". If I rename it to \"SomeOtherName\" it just does not get loaded.

initWithContentsOfURL returns nil and:

相关标签:
6条回答
  • 2020-12-29 10:16

    The most likely cause is that new name is not being included in the build target. Check the target for included files and make sure the new name is there. If not, add it. If the old file name is there remove it before adding the new one.

    If it is in the build target, confirm that the new file retains the .xcdatamodel extension. Check the built product bundle to confirm that it includes a .mom or .momd file with the new name.

    Your mergedModelFromBundles: should look like:

    NSManagedObjectModel *mom=[NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]];
    

    ... or:

    NSManagedObjectModel *mom=[NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObjects:[NSBundle mainBundle],nil] ]; 
    
    0 讨论(0)
  • 2020-12-29 10:19

    Using Xcode 7.2.1:

    1. Go to Project Navigator
    2. Select the data model file (e.g. MyProject.xcdatamodeld)
    3. Select that file's File Inspector
    4. Change the Model Version
    0 讨论(0)
  • 2020-12-29 10:24

    I just ran into the same problem. Here is how I solved it:

    Renaming the model file alone is not enough, because it does not rename the reference to the current model version.

    It turns out the model version is stored in a separate plist file. Simply open it in a text editor and change the old name to your new model file name.

    File: YourNEWModelFile.xcdatamodeld/.xccurrentversion

    <plist version="1.0">
      <dict>
         <key>_XCCurrentVersionName</key>
         <string>YourModelFile.xcdatamodel</string>  <-- Change this to YourNEWModelFile
      </dict>
    </plist>
    

    Please note that you should only do this if you rename the model file during development. For migrating your data model to a new version, follow the Core Data docs.

    0 讨论(0)
  • 2020-12-29 10:38

    This worked for me:

    1. Click on your YourName.xcdatamodel
    2. Right Click on it to show file inspector
    3. On the right you can see "Target Membership", there should be a checkmark.

    (Check if the name in AppDelegate is the same as your xcdatamodel file. -> let container = NSPersistentContainer(name: "YourName") ......)

    0 讨论(0)
  • 2020-12-29 10:39

    I ran into this issue recently when I added a new attribute to my data model entity.

    I had to do the following:

    1. Copy out the contents of my Entity+CoreDataClass.h and Entity+CoreDataClass.m files to a text editor, in order to save the custom properties I had created for those the class (e.g. calculated properties)
    2. Delete the 4 files that XCode generated for me: Entity+CoreDataClass.h, Entity+CoreDataClass.m, StockEntity+CoreDataProperties.h, & StockEntity+CoreDataProperties.m
    3. Add the new attributes to the entity in the .xcdatamodelId file
    4. Select the entity from the list and select Editor -> Create NSManagedObject Subclass...
    5. Add the calculated properties I copied out in step 1 back into the Entity+CoreDataClass.h and Entity+CoreDataClass.m files
    6. Select the Build Phases for my target and delete the Entity+CoreDataClass.m file from the Compile Sources list
    7. Add the .xcdatamoelId file to the Compile Sources list.

    I was then able to build and run the project successfully.

    0 讨论(0)
  • 2020-12-29 10:40

    How does your Core Data initialization code look like? It should look like this:

    NSManagedObjectModel *managedObjectModel = nil;
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SomeOtherName" withExtension:@"mom"];
    
    NSAssert(modelURL != nil);
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    
    0 讨论(0)
提交回复
热议问题