Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an NSDate in a Core Data managed Object converted to NSTimeInterval?

Tags:

I have an object with a property with a Date type defined in my xcdatamodeld object. Why has it generated the NSManagedObject class with a NSTimeInterval? And how do I set a NSDate on it and then get an NSDate back off it?

like image 319
Mike S Avatar asked Jul 27 '11 08:07

Mike S


People also ask

What is Nsmanagedobjectcontext in core data?

An object space to manipulate and track changes to managed objects.

How do I save an object in Coredata?

To save an object with Core Data, you can simply create a new instance of the NSManagedObject subclass and save the managed context. In the code above, we've created a new Person instance and saved it locally using Core Data.

What is managed object in Swift?

Overview. A managed object has an associated entity description ( NSEntityDescription ) that provides metadata about the object, including the name of the entity that the object represents and the names of its attributes and relationships.


3 Answers

I get this if I check the "Use scalar properties for primitive data types" checkbox when I'm generating my files.

This is because NSTimeInterval is a double in disguise, whereas NSDate is a class that inherits from NSObject.

like image 93
teedyay Avatar answered Oct 27 '22 09:10

teedyay


If you want to have mixed entities that uses both scalar and non-scalar property representations such as int32_t for integer or boolean properties but NSDate* for date properties, you need to manually edit the generated managed object .h file.

Therefore I usually start with creating managed object classes by checking the option Use scalar properties for primitive data types (as I have more integer and boolean properties than dates). Then I edit the created header file as follows:

MYEntity.h

@interface MYEntity : NSManagedObject
@property (nonatomic) int32_t index;
@property (nonatomic) NSTimeInterval date;
@end

then becomes:

@interface MYEntity : NSManagedObject
@property (nonatomic) int32_t index;
@property (nonatomic, retain) NSDate * date;
@end

If you are of the kind who doesn't like to manually edit this file, you can also choose to add a convenience property using a category that allows for accessing the scalar property as if it would be an object.

MYEntity+Convenience.h:

@interface MYEntity (Convenience)
@property (nonatomic, readwrite) NSDate *theDate
@end

MYEntity+Convenience.h:

@implementation MYEntity (Convenience)

- (NSDate *)theDate {
    return [NSDate dateWithTimeIntervalSinceReferenceDate:self.date];
}

- (void)setTheDate:(NSDate *)theDate {
    self.date = [theDate timeIntervalSinceReferenceDate]
}

@end

To make the code more readable in this example I would name the original property dateTimeInterval in the data model designer and the convenience property with its desired name: date.

The second approach obviously also works vice versa. You can export your entities with non-scalar properties and then provide a convenience scalar property whose getter and setter accesses the original non-scalar property.

like image 31
Lars Blumberg Avatar answered Oct 27 '22 10:10

Lars Blumberg


Edit: apparently the dateWithTimeIntervalSince1970 is for NSDateFormatter only. NSDate uses a different reference than NSDateFormatter, which as explained in the comments is in 2001. Sorry for my ignorance.

If you have "Use scalar properties for primitive data types" selected when you generated the NSManagedObject subclasses for your entities, it will use scalar properties like int and float instead of NSNumber. This includes NSDate; It will turn NSDate into an NSTimeInterval which is just a double. This NSTimeInterval is a time interval since the 1970 standard epoch used by Apple (if it's before 1970 it probably is negative).

It's pretty simple to convert the NSTimeInterval back into an NSDate if you need it to, but if you don't have a huge database, then you might not want to bother selecting that checkbox.

To convert this time interval into an NSDate just use [NSDate dateWithTimeIntervalSince1970: timeInterval]; where timeInterval is what you get from the database. This can be put in your NSManagedObject subclass so that when you get that property, you get an NSDate anyway.

like image 32
shim Avatar answered Oct 27 '22 11:10

shim