Core Data and Runtime Key Value Coding

I had the impression that when encoding with a key, I could set any undefined attribute to NSManagedObject at run time, and no exception would be thrown, but that would be a way to hold objects attached to a model that are not in the data model.

For example, I have a Foo object that does not have the "bar" attribute. I, although this is at runtime, I could set it like this:

Foo *foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext:ManagedObjectContext];
[foo setValue:@"foobar" forUndefinedKey:@"bar"];

Then I expected that the ManagedObjectContext will keep this value until I request it later, but saving in a managed object context will not save the value of the string.

A famous bug keeps popping up when I run this code:

the entity Foo is not key value coding-compliant for the key "bar"

.. so my question is: what am I not getting or doing wrong?

+3
source share
2 answers

This question, subclasses of NSManagedObject and setValuesForKeysWithDictionary:, may matter.

Conceptually, I don't think you can safely assign random values ​​and keys. This can lead to a violation of the schedule of the object.

+1
source

In your data model, you can set the attributes as Transitional

This allows you to set this attribute, but it will not be stored in the repository, but will be available for requests and cancellations.

http://2pi.dk/tech/cocoa/transient_properties.html

+1
source

Source: https://habr.com/ru/post/1730621/


All Articles