SQLite.swift icon indicating copy to clipboard operation
SQLite.swift copied to clipboard

NSDate fetch crash on some dates

Open vmirgorod opened this issue 9 years ago • 3 comments

Possible it is better to use optional there ... 2015-12-22 09 11 23

vmirgorod avatar Dec 22 '15 03:12 vmirgorod

I am running into this same problem.

jfbomber avatar Jun 01 '16 18:06 jfbomber

it can be possible, but somebody else have to redesign whole Value methods for it.

You could use custom DateFormatter to pass timeIntervalSince1970

iNoles avatar Jun 01 '16 19:06 iNoles

I had the same issue and the fix, for me at least, was a bit subtle.

So I noticed the crash would happen despite stringValue being an acceptable date string. This didn't make sense. Why would it crash when given a perfectly acceptable date value? Turns out this insight was a big clue:

What was actually happening was dateFormatter was causing the problem. Specifically, dateFormatter was being given a different format than the passed in string a bit later in my code, as in:

dateFormatter.dateFormat = "MMM d, yyyy"

This new format would then be applied to the one in SQLite > Foundation.swift, which would, when I had a date with a different format get selected, cause the crash.

In other words I'd give it a string just like yours, but the formatter expected a string in my new format. The date conversion would fail and the optional unwrap would cause a crash.

The fix then, again, for me at least, was to simply set the proper format before the database column was accessed. For example:

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"

..Access database column..

if let lastRefreshedValue = selectrow[_lastRefresh] {

etc...

Alternatively, we can search for an remove any calls to dateFormatter.dateFormat in our project.

MattGrdinic avatar Apr 28 '17 21:04 MattGrdinic