Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unconsistent behaviour when converting data types into Swift objects #34

Closed
ilyavelilyaev opened this issue May 26, 2017 · 1 comment
Closed

Comments

@ilyavelilyaev
Copy link

  • Xcode version: 8.3.2
  • Firebase SDK version: 4.0.0
  • Firebase Product: database

The problem

The way some data stored in Firebase Database is converted into Swift objects is unconsistent.

Steps to reproduce:

For example, i have data stored in Firebase Database like this (JSON)

"test-dict": {
  "2": true,
  "3": true,
  "4": true
},

when i am trying to cast this in Swift to Dictionary [String: Any] or [String: Bool] it always fails.
the actual type in swift for this will be Array [Any]
particular for this example the array will be: [NSNull, NSNull, Bool, Bool, Bool]

but when we add another literal key to the dict:

"test-dict": {
  "2": true,
  "3": true,
  "4": true,
  "a": true
},

it successfully casts to [String: Any] or [String: Bool]

Was this behaviour supposed to be so or is it an issue?

@asciimike
Copy link

asciimike commented May 26, 2017

This is the intended behavior as the Realtime Database coerces objects with numerical keys to arrays. Specifically, "... if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase clients will render it as an array." Once you introduce a non-numerical key, the clients know it's an object again.

In practice, that's why you're seeing: [NSNull, NSNull, Bool, Bool, Bool]. The Database treats it as:

"test-dict": {
  "0:" null,
  "1": null,
  "2": true,
  "3": true,
  "4": true
}

See https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html for more detail

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants