1

I am trying to compare two dates, one of them is an older fetched value, if it's smaller then Date() I am storing Date() as the latestConfirmedDate. If there is no internet connection, the fetched value wouldn't get updates of course.

let latestConfirmedDate = UserDefaults.standard.object(forKey: "latestConfirmedDate") as? Date
    

    if((latestConfirmedDate) != nil)
    {
        let storedDateInLocalTime = DateFormatter.localizedString(
            from: latestConfirmedDate!,
        dateStyle: .medium,
        timeStyle: .medium)
        
        let currentDateInLocalTime = DateFormatter.localizedString(
            from: Date(),
        dateStyle: .medium,
        timeStyle: .medium)
        
        print(storedDateInLocalTime.description)//Producing the same value each time
        print(currentDateInLocalTime.description)
        if(Date() > latestConfirmedDate!)
        {
            UserDefaults.standard.set(Date(),forKey: "latestConfirmedDate")
         }
      }

In the scenario that there is an internet connection and I am getting a fetched Date object, it stores the fetched value fine in UserDefaults.standard.object(forKey: "latestConfirmedDate") . But when entering the second If block it doesn't store the current Date().

The weird thing is that the stored value is always staying as the old fetched value, even though I am using UserDefaults.standard.set(Date(),forKey: "latestConfirmedDate") and the compiler does enters this If block !

17 Nov 2020 at 18:05:58//Stored value, is the old fetched value even though I ran the function a second ago
17 Nov 2020 at 18:29:41//Date()

Where is the problem here ?

ChesterK2
  • 320
  • 3
  • 11
  • Does this answer your question? [Convert string to date in Swift](https://stackoverflow.com/questions/36861732/convert-string-to-date-in-swift) – 0-1 Nov 17 '20 at 17:21
  • No, I am comparing between Date objects. – ChesterK2 Nov 17 '20 at 17:23
  • Not related to your question but you shouldn't compare optional values in Swift against nil to force unwrap it later. Just use if let to unwrap your optional date. SO remove your line of code `if((latestConfirmedDate) != nil) {` and add `if let` to unwrap `latestConfirmedDate` – Leo Dabus Nov 17 '20 at 17:27
  • The Date() function is always going to return the current date and time so if you're _not_ storing `lastConfirmedDate` as sometime in the future I don't think you're ever going to drop into the body of that if block. Another problem is that you call `Date()` twice when you should probably store that as a local var for all your logic. – Aaron Nov 17 '20 at 17:27
  • @Aaron I don't think that is the issue that would cause a 24s interval. I do agree that OP should store Date to a `var` and use a single instance anyway. – Leo Dabus Nov 17 '20 at 17:28
  • @Aaron it does get inside, if the user didn't change the date manually it checks if Date() is bigger than an older value. I am checking here something, but that's beside the question. I do except you suggestions, should have code it a little different but bare in mind that this is only a small skeleton to demonstrate the problem – ChesterK2 Nov 17 '20 at 17:32
  • @ChesterK2 the store date will always be older than Date(). When comparing two dates it needs to be exactly the same (nanosecond) to be considered equal. What you should probably check is if the stored date is in today – Leo Dabus Nov 17 '20 at 17:33
  • I'm confused, then ;-) I think I read your post incorrectly. – Aaron Nov 17 '20 at 17:34
  • @Leo Dabus that's correct see my updated comment. But that's beside the problem, even though it will almost always enter the second if block, it doesn't update the stored value at all – ChesterK2 Nov 17 '20 at 17:35
  • `if Calendar.current.isDateInToday(latestConfirmedDate) {` – Leo Dabus Nov 17 '20 at 17:36
  • Again, that's not the problem. I am trying to catch a sudden change in date and that's exactly what my exceptions of the code. The only problem is that Date() is not stored within the userDefaults, it stays the same as the latest fetched value. – ChesterK2 Nov 17 '20 at 17:38
  • Note that even is UserDefaults is not synchronised to disk when you retrieve its value it will return it from memory anyway. – Leo Dabus Nov 17 '20 at 17:39
  • How sudden of a change are you expecting? – Aaron Nov 17 '20 at 17:40
  • @Aaron just any manually set older date than the last confirmed one – ChesterK2 Nov 17 '20 at 17:41
  • again `if Date() > latestConfirmedDate {` will always be true – Leo Dabus Nov 17 '20 at 17:41
  • That's fine, unless the user manually changed the device date isn't it ? – ChesterK2 Nov 17 '20 at 17:42
  • What do you mean? Do yo need to make sure you have a valid date? You should use a time server. – Leo Dabus Nov 17 '20 at 17:43
  • That will not work while offline. My problem here is that I am printing the values and clearly see that the stored value is never being updated with Date() even though as you stated correctly, its (almost) always enters the second if block. – ChesterK2 Nov 17 '20 at 17:44
  • https://stackoverflow.com/a/63129033/2303865 – Leo Dabus Nov 17 '20 at 18:01
  • Where else in your application might you be setting this date object into that key in UserDefaults? – Aaron Nov 17 '20 at 18:01

0 Answers0