In my NSManagedObject
subclass, I've written a set of custom accessor methods to expose a public CMTime
property called videoDuration
which is backed by an NSData
attribute called videoDurationData
...
- (CMTime)videoDuration
{
[self willAccessValueForKey:@"videoDuration"];
NSValue *videoDurationValue = [self primitiveVideoDuration];
[self didAccessValueForKey:@"videoDuration"];
if (nil == videoDurationValue) {
NSData *videoDurationData = [self videoDurationData];
if (nil != videoDurationData) {
videoDurationValue = [NSValue valueWithCMTimeData:videoDurationData];
[self setPrimitiveVideoDuration:videoDurationValue];
}
}
return [videoDurationValue CMTimeValue];
}
- (void)setVideoDuration:(CMTime)videoDuration
{
[self willChangeValueForKey:@"videoDuration"];
NSValue *videoDurationValue = [NSValue valueWithCMTime:videoDuration];
[self setPrimitiveVideoDuration:videoDurationValue];
[self didChangeValueForKey:@"videoDuration"];
[self setVideoDurationData:[NSData dataWithValue:videoDurationValue]];
}
- (NSValue *)primitiveVideoDuration
{
NSData *videoDurationData = [self videoDurationData];
if (nil != videoDurationData) {
NSValue *videoDurationValue = [NSValue valueWithCMTimeData:videoDurationData];
return videoDurationValue;
}
return nil;
}
- (void)setPrimitiveVideoDuration:(NSValue *)primitiveVideoDuration
{
if (nil != primitiveVideoDuration) {
NSData *videoDurationData = [NSData dataWithValue:primitiveVideoDuration];
[self setVideoDurationData:videoDurationData];
}
else {
[self setVideoDurationData:nil];
}
}
After adding in undo/redo support, my app crashes on -[NSManagedObjectContext save:]
with a EXC_BAD_ACCESS memory exception. However, if I comment out the primitive accessor methods (and have the -primitiveVideoDuration
method simply return nil
, then everything works as expected. I'm guessing that this has something to do with CMTime
not being a key-value compliant scalar structure? (see update below)
UPDATE -- 5/8/12: This thread and the key-value coding programming guide seem to suggest that KVC now supports arbitrary struct data, which would lead me to believe that what I'm trying to do is indeed possible?
UPDATE 2 -- 5/8/12: Evidently, by simply not implementing the primitive accessors, everything works correctly. Why this is the case is still baffling to me...