I'm trying to do a sample application with firebase and I don't have quite understand how I should retrieve nested-flattered data.
Let's suppose I have a db like this
{
"users": {
"user-1": {
"email": "email1",
"matches": [
"match-1",
"match-2"
]
},
"user-2": {
"email": "email2",
"matches": [
"match-1",
"match-2"
]
},
"user-3": {
"email": "email3",
"matches": [
"match-2"
]
}
},
"matches": {
"match-1": {
"name": "Match 1",
"users": [
"user-1",
"user-2"
]
},
"match-2": {
"name": "Match 2",
"users": [
"user-1",
"user-2",
"user-3"
]
}
}
}
and I want to get all the match of the user-1. What I'm doing now is observe users.user-1.matches to get the matches list and then observe every match so the final flow is:
- observe users.user-1.matches
- observe matches.match-1
- observe matches.match-2
- ...
The question is: how can I optimize this flow? (like making something like the sql join)
My idea is to get something like this
{
"users": {
"user-1": {
"email": "email1",
"matches": {
"match-1": {
"name": "Match 1",
"users": [
"user-1",
"user-2"
]
},
"match-2": {
"name": "Match 2",
"users": [
"user-1",
"user-2",
"user-3"
]
}
}
}
}
}
So I can observer the whole structure at once.
I'm using firebase on iOS with swift but feel free to reply in every language you like.
Thanks.