Xcode generate NSManagedObject Subclass issue - ios

After I set up Core Data Stack, I generated the NSManagedObject Subclass automatically.
However, I received the error in xcode 8, which is very annoying.

So I also ran in this issue. You have to disable automatical code generation if you want to create the NSManagedObjects by yourself. So there are different ways. First way is go to your model and changed the Toolversion to xCode 7.3.
In every entity you can set the Codegen to Manual/None like in the Screenshot.
Clean your project, and try again!
I hope this was helpful.

Related

Core Data causing a crash

I am using the following code:
let context = appDelegate.persistentContainer.viewContext
When this line is run, not only does the app crash, xcode also crashes if the device is in debug mode linked to xcode.
I have de-commented out my code line by line and this is the line where it seems to be having problems.
Does anyone know how to fix this?
Thanks.
Was this code working previously for you, and now it has suddenly started crashing? When this has happened to me, it's always been because I made a change to my xcdatamodel without properly migrating it.
The quick fix is to rename your MyAppName.xcdatamodeld file. ie, change it to MyAppName2.xcdatamodeld. Then be sure to change it wherever it appears in your code, such as strings:
var coreDataStack = CoreDataStack(modelName: "MyAppName2")
Once you've got it working again, be sure to look up automatic lightweight migration of Core Data. Basically, any time you want to make changes to Core Data entities, you have to create a new version of your data model first. See this link for more info. iPhone Core Data "Automatic Lightweight Migration"
if you changed in model with Xcode 9.2 please make sure target of that model is added to project in Xcode 9.2 we have to add as target manually

Creating NSManagedObject classes in xcode8?

How to create NsmanagedObject classes in xcode8, it is causing "no such file or directory" while making archieve, which is the best way to create and to use by using xcode 8, please need exact way step by step.
Have you tried googling this? If you did, you would've found tutorials like the one listed below.
https://www.raywenderlich.com/145809/getting-started-core-data-tutorial

Core Data Xcode 8

I started new project without core data checked and then I tried to put it in manually. So everything is fine but I have a question concerning Codegen in Data Model Inspector.
When I put class definition in Codegen field my class was redefined in appropriate to core data way so I deleted my old one. And after I saw extension of this new class where I could find all the properties.
So after I closed I couldn't find it in my project but I want to see it again.
How can I make it appear again?
When an NSManagedObjectModel is configured to generate code, it doesn't add that code to your project. Rather, it generates that code into your Derived Data, in the DerivedSources directory for the target the model is a part of.
In Objective-C, you can just use #import "ModelName+ManagedObjectModel.h" in your other code to gain access any of the entities for which code has been generated. In Swift, you don't even need to do that, you can just use the classes that were generated.
If you want to see the code for those classes, you can use Open Quickly (command-shift-O) and type in one of the class names. Xcode should take you right to the generated source code for it.

Xcode UI does not show the Core Data entities I have created

Xcode Core Data UI does not show the entities I created in graph view. Why is this? How can I fix it?
The graph editor is quirky at the best of times. Restarting Xcode frequently fixes the issue. Switching from the table style to the graph style can force a redraw. Closing the file (not the project, just the model file) and reopening it can also clear this up.
If neither of those are working then you probably have a corrupted xcshareddata file in your Xcode project. Try removing that file (or renaming it to test) while Xcode is not running and then launch Xcode and force it to rebuild it.

Better way to recreate the class definition after modifying core data model?

When designing the core data model in XCode, you can automatically generate NSManagedObject subclass definitions (.m and .h files) by
Selecting the Entities
Choosing "Create NSManagedObject Subclasses" from EDITOR menu
After that, you may add a lot of code in these classes, what if you have to modify the data model setting a lot for some reason after that? To reflect these changes on the data model, is there any automatic way to do that? or you have to do everything manually.
Currently if I try to recreate these class definition from EDIT menu again(automatically), it will replace all the current files. All added code will disappear.
I really hope future version of Xcode can add a smart feature: automatically updating the default class definition without losing the added work. Maybe I am too lazy. :)
You're running into a common problem. You're pretty much stuck with that way of creating managed object subclasses with Xcode for the time being. Knowing that, you can either:
Design around it
For simple cases, you can use Categories to add functionality (though not state) to your NSManagedObject subclasses. Code in the category's file is obviously safe from being overwritten every time your data model changes.
Don't use Xcode
Mogenerator is a nifty tool designed to solve exactly that problem. It creates two classes for each entity instead of one, allowing Xcode to manage one while you manage the other.
It seems Apple has addressed the issue with XCode 7 : now it automatically creates the entity and a category of the entity with its core data properties. When you regenerate, it only updates the category, leaving your custom code in the entity class unharmed. See this link
You can create a class with different name and paste the generated fields into the old class

Resources