Core Data causing a crash - ios

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

Related

What is a clean approach in Xcode 12.2 to add a Core Data file to an older Xcode project?

Update: I fixed this by adding title and date attributes to the Data Model, and also a timestamp attribute required by the persistence class. Compiling I found other errors, that I am currently fixing. Xcode 12 introduces a different structure of the PersistenceController, that I think that simplifies the use, but older code needs a bit of refactoring. I am leaving the post here if it can be of any help.
I am using the most recent Xcode on Big Sur to add CoreData to extend a project that didn't have it, and I am getting this error:
/Users/myroot/Desktop/Development/myapp/app/AppDelegate.swift:61:45: Argument passed to call that takes no arguments
When I try to copy the content of an Xcode 12.2 Persistence.swift example project in my old application project AppDelegate.swift, I get the error in the PersistenceController, in this block of code that includes:
let newItem = Item(context: viewContext)
Has anyone solved how to update a project using AppDelegate.swift with an Xcode 12.2 Core Data code that didn't use it?
Thanks.

Xcode generate NSManagedObject Subclass issue

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.

The model used to open the store is incompatible with the one used to create the store Xcode 7.1

I created a core data model that was working fine until I added a new entity to the model, my code is fine I think... because Xcode can't detect any errors, but as soon as I run my app on the simulator the compiler gives me an error saying
reason=The model used to open the store is incompatible with the one used to create the store
I don't quite understand what this error means. I did search for references regarding this error but the one I found is outdated it was from Xcode 3.2, I'm currently running Xcode 7.1.1, interestingly enough there's no check mark on none of the replies. Despite that I still tried to use some of those methods and nothing has worked.
Things I tried
Resetting iOS Simulator -> Reset Contents and Settings...
Deleted the [Simulator App Folder]/Document/*.sqlite and ran the project
Have you had the same problem? If so can you help me troubleshoot it? Thanks.
That error means that you changed the model so that it doesn't match the one you were using when you created the app's data. It's the classic sign of changing a data model and not either doing model migration or deleting the app from the phone/simulator (and it's been pretty much the same as long as Core Data has existed, so old information is not irrelevant). Deleting the app from the simulator would normally be all that's needed to run the app again. If it's not working, then you're somehow leaving old data in place.
The best way to delete an app from the simulator is usually the same as you'd delete an app on a real device-- click and hold until the app icon starts moving, then click the "x" on its icon to delete it.
If deleting the app doesn't help, you're using old data some other way. Maybe you have some pre-loaded data built into the app that was configured using an old version of the model?
My favorite solution is to open the sqlite database with a third party app, then open the Z_MetaData table and delete the row found in there.
Works like a charm every time.
It may affect your 'migration' if you do migrations.
We do not, so not an issue.

Xcode going crazy! while coding, loses classes, references and doesn't autocomplete giving often <<error type>>

After I installed the latest version of xcode i'm having a very annoying issue.
While I'm coding, xcode goes nuts. Without me doing anything weird, just typing code, xcode stop recognizing classes.
For example: I want to add UITableViewDelegate to my class, but it doesnt recognize it. After i type it manually sometimes it recognize it and it is shown in purple, some other time it doesnt. But both times it won't really consider it, so if I try to write down a method of that delegate, it won't show it.
Other times if I try to call a variable of a certain class, while trying to autocompleting it, it shows <>.
Other times if I try to call any class, let's say I try to type var test = UIActionSheet, it just shows a few elements in the autocomplete list (raw types, primitives, the classes of my main project, but it doesn't show the majority of classes).
It's like it's missing the documentation and the link with the main frameworks...
I'm working on a simple tabbed app from yesterday and it's the 3rd time i started all over because of this issue, thinking that starting over would fix the issue, but it's not working.
If i open a different project while the issue is going on, the other project works ( but i recoded all over my app so it's not that one the issue, and i also have the same issue on other project... it just doesn't affect 2 project at the same time )
i tried deleting derived data
i tried restarting both xcode and the comp
What's going on?
Here are two screenshoots where you can see what's happening:
I had the same problem earlier.
Exit Xcode and delete Derived Data folder here ~/Library/Developer/Xcode
Restart Xcode and you should see the autocomplete working again.
You have to learn how to take it apart and put back together.
Either reinstall Xcode, if it doesn't help, create a new project.
Then copy source files one by one and see when it breaks.
Freshly install the XCode IDE version 6.1
Use CMD + F to find all _element.
Check that if you named one variable _element
The following code may reproduce the error:
class _element:NSObject {
}
let _element:UIImage = UIImage()
// type _element under this line

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.

Resources