How to fix performance problems when using NSDateFormatter
Unfortunately the majority of web services return dates in the ISO-8601 format and not in time interval. So to parse the date we end up using NSDateFormatter which is extremely slow at doing that.
But there is a way for you to improve date parsing performance, you can use the SQLite library to make the conversion. You may have to write a few more lines to get the same result, but the performance gain is totally worth it.
Let's take this date as an example: 2013-09-07T23:45:00Z
, if you want to convert that to time interval you could do this:
sqlite> SELECT strftime("%s", "2013-09-07T23:45:00Z");
1378597500
We can achieve the same result on iOS using the following code:
sqlite3_stmt *statement = NULL;
sqlite3_prepare_v2(db, "SELECT strftime('%s', ?);", -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [dateString UTF8String], -1, SQLITE_STATIC);
sqlite3_step(statement);
sqlite3_int64 interval = sqlite3_column_int64(statement, 0);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
If you want to compare the results, Anurag Mishradid a script to demonstrate this. In his tests the performance gain was 1400%, or from 106 seconds to just 7.
If you want to read more on how SQLite parses the date in it's C lirabry you can go here.