CoreData and NSCalendarDate/NSDate attributes
CoreData and NSCalendarDate/NSDate attributes
- Subject: CoreData and NSCalendarDate/NSDate attributes
- From: Frank Illenberger <email@hidden>
- Date: Thu, 28 Jul 2005 12:54:19 +0200
Hi,
today I got a bit confused about date attributes in CoreData:
On one hand, the documentation of NSAttributeDescription states that
the attribute values are NSDate objects. On the other hand using
"Copy method implementations to clipboard" in XCode 2.1 on a date
attribute creates the following accessor methods which expect
NSCalendarDate objects:
- (NSCalendarDate *)testDate
{
NSCalendarDate * tmpValue;
[self willAccessValueForKey: @"testDate"];
tmpValue = [self primitiveValueForKey: @"testDate"];
[self didAccessValueForKey: @"testDate"];
return tmpValue;
}
- (void)setTestDate:(NSCalendarDate *)value
{
[self willChangeValueForKey: @"testDate"];
[self setPrimitiveValue: value forKey: @"testDate"];
[self didChangeValueForKey: @"testDate"];
}
Actually, when fetching an object from an SQLite store, all date
attributes contain NSDate objects event if they were once inserted
using NSCalendarDate objects.
In my app I currently need NSCalendarDate objects for my date
attributes because I have a load of calculation categories for this
class.
I tried [NSAttributeDescription
_setAttributeValueClassName:@"NSCalendarDate"] on my date attributes
at model load time which led to funny exceptions.
The only solution I have found so far is to convert all fetched
NSDates into NSCalendarDates in the awakeFromFetch method:
- (void)awakeFromFetch
{
[super awakeFromFetch];
NSDictionary *attributes = [[self entity] attributesByName];
NSEnumerator *attributesEnumerator = [attributes keyEnumerator];
id key;
while (key = [attributesEnumerator nextObject])
{
NSAttributeDescription *attribute = [attributes
objectForKey:key];
if(![attribute isTransient] && [attribute attributeType]
==NSDateAttributeType)
{
NSString *name = [attribute name];
id value = [self primitiveValueForKey:name];
if(value)
[self setPrimitiveValue:[NSCalendarDate
dateWithTimeIntervalSinceReferenceDate:[value
timeIntervalSinceReferenceDate]] forKey:name];
}
}
}
But this has the downside of slowing down the fetch process. (Yes, I
know this code can be optimized :-) )
So does anybody know a better way to make CoreData instantiate
NSCalendarDate objects at fetch time?
Cheers
Frank
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden