开发者

Core Data: NSManagedObjectContext save crashing with unrecognized selector

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:

  1. Add the core data framework.
  2. 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.
  3. 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.
  4. Added the Core Data objects to my app delegate, following this example.
  5. 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 cas开发者_Python百科e.


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 (i.e : 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜