CoreData and NSTimeInterval
CoreData and NSTimeInterval
- Subject: CoreData and NSTimeInterval
- From: Luther Baker <email@hidden>
- Date: Wed, 16 Sep 2009 18:56:13 -0500
I'm creating a timesheet/stopwatch application of sorts for the iPhone.
I am using an NSDate in CoreData and following this pattern:
1) Start:
Session* session = (Session*)[NSEntityDescription
insertNewObjectForEntityForName:@"Session" inManagedObjectContext:
managedObjectContext_];
session.startTime = [NSDate date];
2) Calculate time passed and update label:
NSTimeInterval timeDiff = -1*[session.startTime timeIntervalSinceNow];
cell.textLabel.text = [NSString stringWithFormat:@"%f", timeDiff];
In this case, timeIntervalSinceNow is negative. So, I can fix that - or I
could try
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:session.
startTime];
cell.textLabel.text = [NSString stringWithFormat:@"%f", timeDiff];
I don't like this since it is needlessly creating a lot of dates - and their
could be 100s of these timers running at the same time ... or, does the
first one also create a date under the covers and I'm pre-optimizing or
something too early. Can anyone suggest a better way to do this? For
instance, initially, I was simply using NSTimeInterval:
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
...
NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate] -
startTime;
But I end up struggling with the best way to manage this as part of a
CoreData entity. Should I uses a double? or a Date? I understand that a Date
is implemented as an NSTimeInterval but I can't assign or subtract an
NSTimeInterval directly to my entity.date
session.startTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval timeDiff = [NSDate timeIntervalSinceReferenceDate]
- session.startTime;
Obviously, this won't compile when startTime is a Date object but I think it
illustrates what I'm after. Trying to 'lightly' calculate time passed by
using the start time assigned to an Entity. Maybe the original way is just
fine. I'm just looking for suggestions. I guess I could try running this
inside a profiler. As a side note, is there a better way to print the
stopwatch output to the screen? It seems like overkill to constantly update
a Label's text property ... hundreds, maybe thousands of times especially
when there may be 10 or so timers running on the same screen. Does anyone
have any advice for stopwatch type 'update the time' label behavior? Thanks.
-Luther
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden