2

I used childByAutoId() save the timestamp as an auto-id. I can successfully retrieve the timestamp key that looks like -KJrs03bWbSTXfomqzMW. When I check the key's type, it says AnyObject.

 let timestamps = snap.value.allKeys

 for timestamp in timestamps {
      print(timestamp)   // -KJrs03bWbSTXfomqzMW

      if let t = timestamp as? NSTimeInterval {
          print(NSDate(timeIntervalSince1970: t/1000))
          print("true")
      } else {
          print("false")
      }
  }

However, it goes into false bit.

What am I doing wrong? What is the proper way of converting Firebase timestamps to NSDate?

PS: I remember I read Firebase allows for a global timing for the items with this kind of timestamp. Is there a way that would enable me to get human-readable version of the time, such as 30mins ago, 5 hrs ago directly working with Firebase or should I use NSDateFormatter?


The node looks like:

- -KJrs03bWbSTXfomqzMW
    - "key1": "val"
    - "key2": "val"
senty
  • 12,385
  • 28
  • 130
  • 260
  • 2
    i would instead store the timestamp in the model you have for the key. – Shubhank Jun 10 '16 at 05:42
  • I am sorry I didn't get. What do you mean as in the model I have for key? I edited my question and added my node type in the bottom. I have it as a node-key. Isn't using `childByAutoId` approach the optimum way of storing timestamps as? Do you mean to storing it inside the node, as `["timestamp": timestamp]` – senty Jun 10 '16 at 05:46
  • @EICaptainv2.0 Where should I put it? I was using `let timestamps = snap.value.allKeys` and then using for the for loop above. (I fixed the wrong naming of variable in the question). For loop works fine for it. – senty Jun 10 '16 at 05:53
  • `for timestamp in timestamps { if let snapkey = snapshot.key as? String {print("true") }` – Bhavin Bhadani Jun 10 '16 at 05:57
  • Yes, it does return "true" if I try this. – senty Jun 10 '16 at 05:59
  • @senty because its String and you can''t convert it to NSDate as it doesn't have any date formation – Bhavin Bhadani Jun 10 '16 at 05:59
  • @EICaptainv2.0 Should I convert the timestamp as shown in [this answer](http://stackoverflow.com/a/29905645/4705339) and store the timestamp as `"timestamp": timestamp` as a separate key:value? – senty Jun 10 '16 at 06:04
  • @senty no you can't because you don't have any date formation like `2016-06-10 06:05:54 +0000` or miliseconds , etc. – Bhavin Bhadani Jun 10 '16 at 06:06
  • 2
    @senty you can check this ... http://stackoverflow.com/questions/29243060/trying-to-convert-firebase-timestamp-to-nsdate-in-swift?rq=1 – Bhavin Bhadani Jun 10 '16 at 06:11
  • I tried just exactly that in the very beginning, no? As I get multiple node keys (ie timestamped ids), I used a for loop to separate them and the rest is in the question. What am I doing wrong? – senty Jun 10 '16 at 06:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114311/discussion-between-ei-captain-v2-0-and-senty). – Bhavin Bhadani Jun 10 '16 at 06:15
  • @senty Did you ever find a solution to this? Or you switched to using timestamps as El Capitan v2.0 suggested in the chat? – toraritte Jul 09 '18 at 23:00
  • @toraritte unfortunately, i don't remember how I overcame this as it was in 2016 :( – senty Jul 10 '18 at 14:04
  • @senty No worries, it was worth a shot:) Thank you! – toraritte Jul 10 '18 at 15:10
  • @toraritte I'm pretty sure [this answer](https://stackoverflow.com/questions/29243060/trying-to-convert-firebase-timestamp-to-nsdate-in-swift?rq=1) was the answer – senty Jul 11 '18 at 12:51
  • @senty, yeah I saw this above and in your chat. I was just curious whether `childByAutoId` keys could be converted to Date somehow because they are indeed created partially from a client-generated timestamp. Anyway, I figured out a workaround for myself. Thanks again and have a great day! – toraritte Jul 11 '18 at 14:59
  • @toraritte would be nice if you can add it as an answer :) – senty Jul 11 '18 at 15:07
  • The problem is that it does not answer the actual question. Basically my plan is just adding nodes with `childByAutoId` (that takes care of having them in chronological order) and the node would contain all the data with an extra timestamp attribute (using `ServerValue.timestamp`) just in case. – toraritte Jul 11 '18 at 20:58

0 Answers0