I'm trying to add Core Data to an existing application, which isn't easy considering that all the documentation and every tutorial starts out with creating an app that uses core data from the start. So I'm trying to convert an existing model class to be a core data entity. Here's what I did:
- Add the core data framework.
- Add an xcdatamodel file. It showed me a dialog where I could add an existing class, so I added my model. Then I tweaked the attributes and told it what the types were.
- Instead of generating a new model class (since I already have one) I created a test project to see what it would look like, and adjusted my class to match. This included changing it to inherit from NSManagedObject, changing my properties to dynamic, removing release calls, etc.
- Added the Core Data objects to my app delegate, following this example.
In my ViewController, where I used to alloc my model, I changed it to
MyModel *model = (MyModel*)[NSEntityDescription insertNewObjectForEntityForName:@"MyModel"] inManagedObjectContext:[delegate managedObjectContext];
Note that delegate is a reference to my app delegate, declared previously. Perhaps that's not the smart way to do it.
After setting all the properties, I have:
[[delegate managedObjectContext] save:&error];
This line crashes, and a backtrace says that it's inside [NSSqlLiteConnection execute]
, about 8 levels inside the save function. The exception is:
*-[NSConcreteValue UTF8String]: unrecognized selector sent to instance*
What is this concrete value? And why is this being called, by whom? If it matters, my model creating / saving code is inside a function that's a callback for an NSNotification. Is that on a separate thread? I heard that the managedObjectContext is not thread safe. But I'm not getting the same error I would expect in that case.