将“long”类型存储在firebase中 [英] Storing 'long' type in firebase

查看:123
本文介绍了将“long”类型存储在firebase中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个由Core Data支持的iPhone应用程序。我们在我们的核心数据存储中使用int64,我想知道是否需要做任何特殊的事情来将数字存储在firebase中。我想知道这一点,JavaScript不支持64位无符号整数。我们也正在编写​​一个JavaScript应用程序,它必须读取这个数字。



我能想到的一个方法是将其存储为字符串,然后将其转换为int64 iPhone客户端。然而,这似乎有点乏味, firedata 似乎并不直接支持这种翻译。我们还必须在Firebase中添加对此属性的验证 - 所以验证将是,而不是一个数字,它将是一个只有数字的字符串。



有没有人遇到这些问题以及对于这个问题推荐的方法是什么?

解决方案

这确实是一个棘手的情况。首先,为了使情况变得清晰(我认为你知道这一点,但只是为了避免混淆):


  • Firebase can / will精确地存储64位整数。

  • Android和iOS客户端可以交互(读写)64位整数,而不会出现问题。数字为64位浮点数,这意味着它只能精确地表示高达〜2 ^ 52的整数。 2 ^ 52和2 ^ 64之间的整数可能会失去精度(四舍五入为可用64位浮点数表示的最接近的整数)。


    至于你的选择,我怀疑你有一个很好的处理这些,但你可以:




    • 容忍损失的精度。有时候,这没关系。例如,也许你可以保证你不会在2 ^ 52以上存储整数,所以精度的损失实际上不成问题。或者,也许你只需要查看来自JS的数据,一些舍入是可以的。有一点需要注意的是,如果您从JS中读取数据并将其写回,数据将被写回到Firebase中,从而导致精度下降。

    • 存储数字作为字符串。正如您所建议的,您可以将该号码存储为字符串。但这样做可能不方便,并且限制了您在安全规则中的验证。

    • 将数字拆分为2个32位整数。高32位与低32位分开。这可能是不方便的,但可以让你保持精确度,并在安全规则中进行数字验证。 ,但是这些是立即想到的。希望这有助于!


      We have an iPhone application that is backed by Core Data. We use int64 in our core data store and I am wondering if we need to do anything special to store the number in firebase. I am wondering about this as javascript does not support 64 bit unsigned integers. We also are writing a javascript app that will have to read this number.

      One approach I can think of is to store this as string and then convert it to int64 on the iPhone client. However this seems little bit tedious and firedata does not seem to support such translations directly. We will also have to add validation on this attribute in Firebase - so the validation would be that instead of a number it will be a string with only digits.

      Has anyone encountered these issues and what would be the recommended approach for this issue ?

      解决方案

      This can indeed be a tricky situation. First just to make the situation clear (I think you are aware of this, but just to avoid any confusion):

      • Firebase can/will store 64-bit integers precisely.
      • Android and iOS clients can interact (read and write) 64-bit integers without issue.
      • JavaScript stores all numbers as 64-bit floating point numbers, meaning it can only represent integers up to ~2^52 precisely. Integers between 2^52 and 2^64 may lose precision (get rounded to the nearest integer representable by a 64-bit float).

      As for your options, I suspect you have a good handle on these as well, but you could:

      • Tolerate the loss of precision. Sometimes this is okay. For example, maybe you can guarantee you won't store integers over 2^52, so the loss of precision won't actually be an issue. Or maybe you only need to view the data from JS and some rounding is okay. One thing to be careful of is if you read the data from JS and write it back, the data will be written back to Firebase with a loss in precision.
      • Store the numbers as strings. As you suggested, you can just store the number as a string. But this can be inconvenient and does limit the validation you can do in the security rules.
      • Split the number into 2 32-bit integers. You could store the upper 32-bits separately from the lower 32-bits. This is probably inconvenient, but would allow you to keep precision and also do some numeric validation in security rules.

      There may be other options, but these are the ones that immediately come to mind. Hope this helps!

      这篇关于将“long”类型存储在firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆