0

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.

D-A UK
  • 1,084
  • 2
  • 11
  • 24

2 Answers2

1

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"

cmilr
  • 379
  • 4
  • 13
  • Thank you for your answer. Yes, the code was working previously for me but has started crashing. And yes I made a change to my xcdatamodel. It seems like I have exactly the same conditions for error than you have had but fixed - How do you go about fixing it? - Thanks – D-A UK Mar 24 '18 at 07:56
  • I've added the fix to my answer. Good luck!! – cmilr Mar 24 '18 at 16:34
0

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

Bhargav Sejpal
  • 1,352
  • 16
  • 23