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:
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.
Well, I'm pretty sure you have a property declared as NSString on your database model (.xcdatamodel) that is declared as something else (a NSNumber maybe) on your object model. When compiling your code, no warnings are generated but when CoreData tries to save the moc (ie : writing data on the persistent store, AKA your SQLite database), it fails when CoreData tries to convert your supposed NSString to UTF8 encoding.
You should double-check your properties on both the database model and object model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.