0

In my code dateFormatter.date is used multiple time in loop (more than 100) and i am getting crash sometime even if input date is in correct format. Getting date nil (sometimes).

getDayFromDate() function in in loop and passing different date received from web-service (e.g 2018-06-17T00:00:00) as a parameter. always passing time as 00:00:00. inputDateString is checked and it is always in correct format.

    extension Formatter {
    static let enUSPOSIX: DateFormatter = {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")//For 11.3.1 version change
        return formatter
    }()
}

func getDayFromDate(inputDateString: String) -> Int{
    let dateFormatter = Formatter.enUSPOSIX

    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    let date = dateFormatter.date(from: inputDateString )    //  e.g. 2018-06-17T00:00:00
    dateFormatter.timeZone = TimeZone(identifier: "UTC")
    let calendar = Calendar.current
    let dayComponent = calendar.component(.day, from: date!)
    return dayComponent

}

I observed that when there is too many background apps running in iPhone then only it get crashing.

AVR
  • 119
  • 1
  • 11
  • Works fine on the playground for me. Although i would advise you not to force unwrap in cases where there is chance for the variable to be nil. Safely unwrap with if-let or guard statements. – Rakesha Shastri Sep 18 '18 at 09:31
  • @RakeshaShastri working fine for me too on playground but getting crash in application but not always (sometime) i dont know why it is crashing on some launch and with another works fine. in app this code is getting executed more than 100 times on main thread. – AVR Sep 18 '18 at 09:38
  • If you do not provide context how would we know why it is getting executed 100 times on the main thread. Provide the context for your code. Where is it being run? – Rakesha Shastri Sep 18 '18 at 09:39
  • It looks like a duplicate of https://stackoverflow.com/questions/52178316/why-date-formatter-crashes? The given date format and date string must not crash. Most likely the issue is somewhere else. Did you try `ISO8601DateFormatter` as suggested in your previous question? – vadian Sep 18 '18 at 09:40
  • let date = dateFormatter.date(from: "2018-06-17T00:00:00") did you get date on this line put break point and check it. – Himanshu Moradiya Sep 18 '18 at 09:41
  • @vadian `ISO8601DateFormatter only` `available ios>=10` & I observed that when there is too many background apps running in iPhone then only it get crashing. – AVR Sep 18 '18 at 09:58
  • As requested by another comment, please provide code showing how you loop over your dates. It is not clear from your sample which parts are executed multiple times (or maybe even from different threads?). – dr_barto Sep 18 '18 at 10:40
  • @dr_barto check updated question. the whole process is in main thread – AVR Sep 18 '18 at 10:59
  • Creating date formatters continuously is [really expensive](http://www.chibicode.org/?p=41). Make one and hold on to it. – Code Different Sep 18 '18 at 14:24

1 Answers1

0

Do not use forced unwrap while writting the code. If your variable hold nil value it will crash

change your code

let dayComponent = calendar.component(.day, from: date!)

to

if let convertedDate = date {
  let dayComponent = calendar.component(.day, from: convertedDate)
}
Shruti
  • 1,849
  • 1
  • 13
  • 21