0

I have an entity called TimeInterval whose only attributes are a startDate and finishDate, their type is Date. I obviously do not need to add another attribute called totalTime because that can be calculated by doing: [finishDate timeIntervalSinceDate: startDate]

Can I create a fetched property for the attribute totalTime? If not then what is the best way to go about this without having to add totalTime as an attribute as that seems redundant.

I am new to Core-Data by the way.

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • perhaps a category on `TimeInterval` to add a method for calculating and returning 'totalTime' ? – pnizzle Aug 25 '15 at 02:18

4 Answers4

1

You can do this with a separate property (or a full-on transient attribute if you like).

Consider something like this...

@interface Item : NSManagedObject
@property (nonatomic, readonly, assign) NSTimeInterval totalTime;
@end


@implementation Item
- (NSTimeInterval)totalTime
{
    [self willAccessValueForKey:@"totalTime"];
    NSDate *finishDate = [self primitiveFinishDate];
    NSDate *startDate = [self primitiveStartDate];
    NSTimeInterval result = [finishDate timeIntervalSinceDate: startDate];
    [self didAccessValueForKey:@"totalTime"];
    return result;
}
@end
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • good idea with the transient property, indeed, hadn't thought of that one. I wouldn't do that though as I don't want the model to be decisive (make another developer think that the total time is actually saved). I also wouldn't add the property directly to the Entity class and the class can be recreated at a future point, hence a subclass, or category (which I prefer in this case). – pnizzle Aug 26 '15 at 01:34
0

Can I create a fetched property for the attribute totalTime? If not then what is the best way to go about this without having to add totalTime as an attribute as that seems redundant.

My Answer is: No, you should not create its fetched property, because you don't want this as an attribute of your entity so you just create a method lets say

-(NSTimeInterval) getTotalTimeWithStartDate:(NSDate *)startDate withFinishDate:(NSDate *)finishDate{
NSTimeInterval interval = [startDate timeIntervalSinceDate:finishDate];
return interval;

}

//and then call this method using

[self getTotalTimeWithStartDate:startDate withFinishDate:finishDate];
Irfan Anwar
  • 1,878
  • 17
  • 30
  • I already stated that I know that time difference can be calculated using `timeIntervalSinceDate `. Your solution works yes but a category suits the situation better, as I have shown in my solution. – pnizzle Aug 25 '15 at 05:50
0

For doing mathematical calculation on different columns of same table, we are basically using NSExpressionDescription, and setting it to NSFetchRequest as below:

NSExpressionDescription *expressionDesc = [[NSExpressionDescription alloc] init];
[expressionDesc setName:@"totalTime"];
NSExpression *expression = [NSExpression expressionForFunction:@"multiply:by:"
                                                    arguments:@[[NSExpression expressionForKeyPath:@"startDate"],
                                                               [NSExpression expressionForKeyPath:@"finishDate"]];
[expressionDesc setExpression:expression];
[expressionDesc setExpressionResultType:NSInteger64AttributeType];

fetchRequest.propertiesToFetch = @[expressionDesc]
Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
  • @Ankit_Thakur I will have a look at your solution soon but looks like overkill for what I need. – pnizzle Aug 25 '15 at 05:48
0

It seems using fetched properties would not work for my situation. A simple category on TimeInterval does the trick:

@implementation TimeInterval (Extras)

-(NSTimeInterval)intervalTime
{
    return [self.finisheDateTime timeIntervalSinceDate: self.startDateTime];
}

@end
pnizzle
  • 6,243
  • 4
  • 52
  • 81