0
votes

From my understanding of Core Data, all that is necessary for primitive accessors to work is the @dynamic directive for the property name (as well as declaring primitive accessors for that property within the entity implementation).

For some reason, when using the generated primitive accessor the setState: method is not modifying the state property:

- (int)state 
{
    NSNumber * tmpValue;

    [self willAccessValueForKey:@"state"];
    tmpValue = [self primitiveState];
    [self didAccessValueForKey:@"state"];

    return [tmpValue intValue];
}

- (void)setState:(int)value 
{
    [self willChangeValueForKey:@"state"];
    [self setPrimitiveState:[NSNumber numberWithInt:value]];
    [self didChangeValueForKey:@"state"];
}

while using the key-value-coding version does modify the state property

- (int)state 
{
    NSNumber * tmpValue;

    [self willAccessValueForKey:@"state"];
    tmpValue = [self primitiveValueForKey:@"state"];
    [self didAccessValueForKey:@"state"];

    return [tmpValue intValue];
}

- (void)setState:(int)value 
{
    [self willChangeValueForKey:@"state"];
    [self setPrimitiveValue:[NSNumber numberWithInt:value] forKey:@"state"];
    [self didChangeValueForKey:@"state"];
}

in both cases, I primitive accessors are declared as follows (and as per Apple's example and code generation):

@interface Post (CoreDataGeneratedPrimitiveAccessors)

- (NSNumber *)primitiveState;
- (void)setPrimitiveState:(NSNumber *)value;

@end

I'm a bit at a loss to why this would be. Any help would be greatly appreciated!

4

4 Answers

3
votes

After tremendous amounts of head-scratching, debugging, fiddling and guess-and-check, I've finally figured out what the problem is: Core Data primitive accessors AREN'T dynamically generated if you define those attributes as instance variables. I had defined them for debugging purposes (as GBD cannot see the values of properties without defined ivars, it seems), and this prevented primitive accessors from being generated correctly. This is something that Apple should really document in some form. As it's very difficult to discover on one's own. I hope this helps others who've been having the same issue!

1
votes

I've been looking into this and one of the things discovered is that, contrary to docs, the implementation file generated from the data model does NOT list the primitive dynamic accessors. Other places state that you have to add them yourself. Could that be the issue?

0
votes

Are you using/modifying the code of an NSManagedObject generated by Xcode? I believe that by default these are generated as "commented" out by an #if 0 directive.

0
votes

Just wanted to say that I am having the same problem and had to switch to setPrimitiveValue and primitiveValueForKey based on your comment here. It bothers me that the default implementation does not work. Of note in my case is that I am subclassing another NSManagedObject. Not sure if that's your case as well.