Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-08-2010, 09:18 AM   #1 (permalink)
Registered Member
 
twild's Avatar
 
Join Date: Apr 2009
Posts: 107
twild is on a distinguished road
Default NSDateFormatter localizedStringFromDate performance

During my configureCellForIndexPath (called by CellForRowAtIndexPath) I am taking a date in GMT and converting it to the user's date/time. I am using NSDateFormatter's localizedStringFromDate method. Everything works, but it is taking a huge chunk of time to format the date. I used the TimeProfiler Instrument and found that this method slows down drawing my cells considerably:

Code:
configureCell time:
----------------------------------------------------
Using localizedStringFromDate: 97 ms

Hard-codeing a date: 8 ms
Is there a faster way to convert the date from GMT/UTC to the user's localized date in real-time? I'd prefer not to store it converted incase they move across timezones.

It runs ok on an iPhone4, but on a 3g scrolling through the UITableView is very choppy. Once I hard-code the date/time, it is very smooth.
twild is offline   Reply With Quote
Old 08-08-2010, 10:04 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by twild View Post
During my configureCellForIndexPath (called by CellForRowAtIndexPath) I am taking a date in GMT and converting it to the user's date/time. I am using NSDateFormatter's localizedStringFromDate method. Everything works, but it is taking a huge chunk of time to format the date. I used the TimeProfiler Instrument and found that this method slows down drawing my cells considerably:

Code:
configureCell time:
----------------------------------------------------
Using localizedStringFromDate: 97 ms

Hard-codeing a date: 8 ms
Is there a faster way to convert the date from GMT/UTC to the user's localized date in real-time? I'd prefer not to store it converted incase they move across timezones.

It runs ok on an iPhone4, but on a 3g scrolling through the UITableView is very choppy. Once I hard-code the date/time, it is very smooth.

97 ms does sound like an awfully long time to do a date format. That's probably just "the cost of doing business", however, and no way around it.

Here's what I would do:

Add an NSString entry to your data model (usually an NSArray) for the formatted date string. Write your -cellForRowAtIndexPath routine to check to see if that string is blank, and fill it in using localizedStringFromDate if it IS blank. To get really fancy, have your cellForRowAtIndexPath display the cell with the date string blank, and generate an async request for a date string that re-displays the cell once the request is complete.

Don't save the formatted date string to disk, always generate it at runtime. That way, if the user changes time zones and relaunches your app, the strings will get regenerated. You can also have your model zero out all these date strings when you get an applicationWillEnterForeground message. (To keep things clean, I would suggest having your app delegate generate an NSNotificationManager notification message, which your table view's model can listen for.)

The final step will be to have your table view's view controller populate the date strings in it's -viewDidLoad method. You could probably get away with loading them all in one shot, synchronously. If you have hundreds of entries in the table you are displaying then you will want to create an NSOperationQueue/NSOperation to do this in the background. Write the routine to check to see if a given date string is blank, and only call localizedStringFromDate if the date string is blank.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-08-2010, 01:48 PM   #3 (permalink)
Registered Member
 
twild's Avatar
 
Join Date: Apr 2009
Posts: 107
twild is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
97 ms does sound like an awfully long time to do a date format. That's probably just "the cost of doing business", however, and no way around it.

Here's what I would do:

Add an NSString entry to your data model (usually an NSArray) for the formatted date string. Write your -cellForRowAtIndexPath routine to check to see if that string is blank, and fill it in using localizedStringFromDate if it IS blank. To get really fancy, have your cellForRowAtIndexPath display the cell with the date string blank, and generate an async request for a date string that re-displays the cell once the request is complete.

Don't save the formatted date string to disk, always generate it at runtime. That way, if the user changes time zones and relaunches your app, the strings will get regenerated. You can also have your model zero out all these date strings when you get an applicationWillEnterForeground message. (To keep things clean, I would suggest having your app delegate generate an NSNotificationManager notification message, which your table view's model can listen for.)

The final step will be to have your table view's view controller populate the date strings in it's -viewDidLoad method. You could probably get away with loading them all in one shot, synchronously. If you have hundreds of entries in the table you are displaying then you will want to create an NSOperationQueue/NSOperation to do this in the background. Write the routine to check to see if a given date string is blank, and only call localizedStringFromDate if the date string is blank.
Excellent, thanks a lot Duncan C!
twild is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 312
8 members and 304 guests
arash5500, HemiMG, linkmx, mediaspree, Objective Zero, Paul Slocum, stanny, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:31 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0