Re: CoreData and NSCalendarDate/NSDate attributes
Re: CoreData and NSCalendarDate/NSDate attributes
- Subject: Re: CoreData and NSCalendarDate/NSDate attributes
- From: Ryan Britton <email@hidden>
- Date: Thu, 28 Jul 2005 08:37:46 -0700
Store it as a binary object instead maybe? [NSUnarchiver
unarchiveObjectWithData:yourBinaryDate] and [NSArchiver
archivedDataWithRootObject:yourCalendarDate]
Other than that, since the Core Data internal API is still private,
there isn't any way I know of to expand the types it can support.
On Jul 28, 2005, at 3:54 AM, Frank Illenberger wrote:
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
_______________________________________________
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