Firebase push uniqueness scope

后端 未结 2 1765
醉话见心
醉话见心 2021-01-14 19:25

The docs say Firebase push generates a unique key, I want to know if this key is unique across my whole database (or even a GuId), or just in the node where it was pushed on

相关标签:
2条回答
  • 2021-01-14 20:01

    It is statistically nearly impossible to get duplicate push-Ids. For it to happen:

    1. The first 48 bit piece of the id, a time stamp generated by the client, would need to be generated in the same millisecond
    2. The next 72 randomly generated bits would need to be the same. So you could have duplicate push ids, but there are 2^72 possible random permutations ( over 4 sextillion which is 4 times the estimated grains of sand on all the beaches in the world ). So you would need a lot of push requests in the same millisecond.

    One of the core developers wrote a blog post describing how they are generated: https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

    "A push ID contains 120 bits of information. The first 48 bits are a timestamp, which both reduces the chance of collision and allows consecutively created push IDs to sort chronologically. The timestamp is followed by 72 bits of randomness, which ensures that even two people creating push IDs at the exact same millisecond are extremely unlikely to generate identical IDs. One caveat to the randomness is that in order to preserve chronological ordering if a client creates multiple push IDs in the same millisecond, we just 'increment' the random bits by one."

    0 讨论(0)
  • 2021-01-14 20:22

    It is unique across the whole database. From the documentation:

    Generate a new child location using a unique name and returns a Firebase reference to it. This is useful when the children of a database location represent a collection of items. See Saving Lists of Data.

    You can optionally pass a value to push() and the value will be immediately written to the generated location. If you don't pass a value to push(), nothing is written and the child will remain empty unless written to using set().

    The unique name generated by push() is prefixed with a client-generated timestamp so that the resulting list will be chronologically-sorted.

    0 讨论(0)
提交回复
热议问题