Can you get the timestamp from a Firebase array key?

后端 未结 2 1903
旧巷少年郎
旧巷少年郎 2020-12-01 18:40

According to this blog post, firebase array keys are created using a timestamp:

It does this by assigning a permanent, unique id based on the current

2条回答
  •  囚心锁ツ
    2020-12-01 18:53

    Here's a version of Frank's code re-written in Swift (4.2 at the time of writing.)

    Just to be clear, my use case for this was to patch my old models with no timestamps (createdAt, updatedAt.) I could just throw in random dates in them just to save me some headaches. But then that wouldn't be relevant to their models. I knew that there's an element of time baked into these auto-ids based on what I've read from other articles.

    let PUSH_CHARS = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
    
    func decode(autoId: String) -> TimeInterval {
      let substring = autoId.substring(toIndex: 8)
      var timestamp = 0
      for i in 0..

    Grab the Playground-ready code here: https://gist.github.com/mkval/501c03cbb66cef12728ed1a19f8713f7.

提交回复
热议问题