Firebase Querying - I Get Some Type of Nsarray as Result (Only Sometimes)

Firebase querying - I get some type of NSArray as result (only sometimes)

I asked same question in forum ..

You should avoid arrays (specifically, numeric, sequential ids) in distributed systems.

For more on how array-like behaviors are handled in Firebase and Why you should avoid arrays:

It clearly defines in old firebase doc., whats going on when you use Arrays in firebase. check Arrays in firebase Database

You can also go through Best Practices: Arrays in Firebase .. where it says

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 will render it as an array.

Accessing nested firebase values

Try listening for

refPerson.child(092830948290384).observeEventType(.Value)

And use:

snapshot.value!["favoriteColors"] 

to get the data you need into a dictionary

Firebase not receiving key values Swift

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 will render it as an array.

So I believe that you always use some string as key rather than Int because sequential manner key often converted in to arrays as firebase believes that its auto incremented ...

More info. check this answer

Retrieve Int in Firebase Database with Swift 3

The problem is that when you use Ints in a sequential way Firebase treats them like arrays.
You should avoid using Ints like keys, because:

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 will render it as an array.

Here you more details about the issue!
And some best practices from the Firebase blog.

Access snapshot data get from firebase database

As you have added response of mySnap.childSnapshot(forPath: "positions").value in comment it is type of Array of Dictionary so type cast it to [[String: Any]].

let positions = mySnap.childSnapshot(forPath: "positions").value as? [[String: Any]]

Is there a workaround for the Firebase Query IN Limit to 10?

I found this to work well for me without needing to make as many queries (loop and request in batches of 10).

export async function getContentById(ids, path) {
// don't run if there aren't any ids or a path for the collection
if (!ids || !ids.length || !path) return [];

const collectionPath = db.collection(path);
const batches = [];

while (ids.length) {
// firestore limits batches to 10
const batch = ids.splice(0, 10);

// add the batch request to to a queue
batches.push(
collectionPath
.where(
firebase.firestore.FieldPath.documentId(),
'in',
[...batch]
)
.get()
.then(results => results.docs.map(result => ({ /* id: result.id, */ ...result.data() }) ))
)
}

// after all of the data is fetched, return it
return Promise.all(batches)
.then(content => content.flat());
}


Related Topics



Leave a reply



Submit